FileNotFoundException while reading an XML file for analysis

I am writing a program in Java where I read data from an XML file and parse it. The file is imported into a folder named "Resources" in the src directory of my project. I am using Eclipse. When I run the program, I get the following error:

java.io.FileNotFoundException: /Users/thechiman/Dropbox/introcs/PSU SOC Crawler/resources/majors_xml_db.xml (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
...

The relevant code is here:

private void parseXML() {
    //Get a factory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    try {
        //Use factory to get a new DocumentBuilder
        DocumentBuilder db = dbf.newDocumentBuilder();

        //Parse the XML file, get DOM representation
        dom = db.parse("resources/majors_xml_db.xml");
    } catch(ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch(SAXException se) {
        se.printStackTrace();
    } catch(IOException ioe) {
        ioe.printStackTrace();
    }
}

I do not understand why I get a FileNotFoundException when there is a file. Thanks for the help.

+5
source share
1 answer

DocumentBuilder.parse(String) URI, URI ( , , "" URI). , . -, , .

, parse, parse(File) parse(InputStream). , .

File ( parse ), , ( File.exists() ..). , DocumentBuilder DOM, . , (, new File("resources/majors_xml_db.xml")), . , , .

+6

All Articles