Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundException when reading .xml file to parse

I'm writing a program in Java where I read in 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'm 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'm getting the FileNotFoundException when the file is there. Thanks for the help.

like image 975
ericso Avatar asked Apr 04 '10 22:04

ericso


People also ask

What causes XML parsing error?

The most common cause is encoding errors. There are several basic approaches to solving this: escaping problematic characters ( < becomes &lt; , & becomes &amp; , etc.), escaping entire blocks of text with CDATA sections, or putting an encoding declaration at the start of the feed.

How do you parse an XML file?

To parse XML documents, use the XML PARSE statement, specifying the XML document that is to be parsed and the processing procedure for handling XML events that occur during parsing, as shown in the following code fragment.

Can XML be parsed?

Every XML application has to parse an XML document before it can access the information in the document and perform further processing. Therefore, XML parsing is a critical component in XML applications.

What are the two methods of parsing in XML document?

To read and update, create and manipulate an XML document, you will need an XML parser. In PHP there are two major types of XML parsers: Tree-Based Parsers. Event-Based Parsers.


1 Answers

With DocumentBuilder.parse(String), the argument is interpreted as a URI, and in this case, it's going to be a relative URI (the string you're giving it is not a "full" URI). What it's relative to is a bit ambiguous at this point, without further information on your setup. The runtime will be interpreting it as relative to something, but it's not clear here what that something is.

You'll get more reliable results by using one of the other parse methods, such as parse(File) or parse(InputStream). In each case, there's no ambiguity as to what you're asking it to parse.

If you decide to construct a File object first (to pass to parse later), then you have a more manageable problem of making sure that that file exists (using File.exists() and so on). If you can't get that far, then your problem isn't with DocumentBuilder or the DOM, it's with basic file paths. Remember that if you use relative file paths (e.g. new File("resources/majors_xml_db.xml")) then this will be resolved relative to the working directory of the process. If it works or not depends on how you launch your program.

like image 103
skaffman Avatar answered Sep 30 '22 22:09

skaffman