adohertyd
adohertyd

Reputation: 2689

Can't access XML file in project directory

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);
    }

}

Upvotes: 0

Views: 2151

Answers (3)

vzamanillo
vzamanillo

Reputation: 10414

Try this

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

It works for me

Netbeans

Upvotes: 2

Franck Ngako
Franck Ngako

Reputation: 157

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));

Upvotes: 1

Franck Ngako
Franck Ngako

Reputation: 157

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

Upvotes: 1

Related Questions