0

Strange situation here and its driving me insane. I have a skeleton XML file in my project root directory and I want to read it into my program and modify it based on the users input. I'm getting a file not found exception and I can't understand why. My project is called customerCreator. The XML file is at:

C:\Users\user\Documents\NetBeansProjects\CustomerCreator\skeleton.xml

My Java source file is at:

C:\Users\user\Documents\NetBeansProjects\CustomerCreator\src\java\org\user\r6CustomerCreator\parsers\XMLParser.java

EDIT: Using the absolute filepath causes the follwing error: java.lang.IllegalArgumentException: InputStream cannot be null

public class XMLParser {

    public XMLParser(Map paramMap) {
        parseXML(paramMap);
    }

    private void parseXML(Map<String, String> paramMap) {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document document = null;

            try {
                builder = builderFactory.newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();  
            }

            try {
                document = builder.parse(new FileInputStream("skeleton.xml"));
            } catch (SAXException | IOException e) {
                e.printStackTrace();
            }

        System.out.print(document);
    }

}
adohertyd
  • 2,689
  • 19
  • 58
  • 78

3 Answers3

2

Try this

document = builder.parse(this.getClass().getResourceAsStream("/resources/skeleton.xml"));

It works for me

Netbeans

vzamanillo
  • 9,905
  • 1
  • 36
  • 56
1

you should try to use the fullpath. If it works it means that your active solution directory is not the one you think.

Franck Ngako
  • 163
  • 6
  • Tried that and got the error: `java.lang.IllegalArgumentException: InputStream cannot be null` but I know for sure that the file is not empty – adohertyd Dec 13 '13 at 16:39
1

well, try to change your file extension and give it another try. If it works i will advice you to use dedicated xml classes in order to read from your file. try .txt extension

also have a look to this. Looks like you should use the following syntax FileInputStream fis = new FileInputStream (new File(NAME_OF_FILE));

Community
  • 1
  • 1
Franck Ngako
  • 163
  • 6