I a folder structure like so:
src\java\com\company\resources\xmlFile.xml
The xml file is in the package com.company.resources. I’m using netbeans, so here is a picture of the structure:

I am trying pass the address of the xml file as a string to this static method found in another jar:
public static String createXMLStringFromDocument(String fileName){
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
InputStream inputStream = new FileInputStream(new File(fileName));
org.w3c.dom.Document doc = documentBuilderFactory
.newDocumentBuilder().parse(inputStream);
StringWriter stw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance()
.newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(stw));
return stw.toString();
} catch (Exception e) {
return e.toString();
}
}
What should be passed in?
This works as expected in another project, run in Eclipse, where I have left the xml file at the ROOT of the project. I pass in the file name, “xmlFile.xml”, and it works. However I can’t seem to get this to work in netbeans. I get a file not found with all the addresses I’ve tried:
“src/java/com/company/resources/xmlFile.xml” etc.
What am I missing here.
Since your xml is on the classpath, you don’t need the full path to it (as it may be hard or impossible to get if the file is in a .jar file). You just need to get a stream to it. Use: