2

I have a weird problem, i have to parse XML file to get data, when I parse file: http://www.nbp.pl/kursy/xml/c073z070413.xml, all works ok (file is parsed), but when I try parse file : http://www.nbp.pl/kursy/xml/a002z020103.xml then I get a message that program cant find this file (In browser, the XML file works)

Exception:

java.io.FileNotFoundException: C:\Projects\AreYouSmart\abch.dtd (Could not found file)

Below is a full example code. (Code is taken from StackOverflow: XML parse file from HTTP)

public class TylkoPobieranie {

    public static void main(String[] args) {
        try {
            new TylkoPobieranie().start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void start() throws Exception {
        // link work: URL url = new URL("http://www.nbp.pl/kursy/xml/c073z070413.xml");
        URL url = new URL("http://www.nbp.pl/kursy/xml/a002z020103.xml");
        URLConnection connection = url.openConnection();

        Document doc = parseXML(connection.getInputStream());
        NodeList descNodes = doc.getElementsByTagName("pozycja");

        for (int i = 0; i < descNodes.getLength(); i++) {
            System.out.println(descNodes.item(i).getTextContent());
        }
    }

    private Document parseXML(InputStream stream) throws Exception {
        DocumentBuilderFactory objDocumentBuilderFactory = null;
        DocumentBuilder objDocumentBuilder = null;
        Document doc = null;
        try {
            objDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
            //ANSWER:
             objDocumentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

            objDocumentBuilder = objDocumentBuilderFactory.newDocumentBuilder();

            doc = objDocumentBuilder.parse(stream);
        } catch (Exception ex) {
            throw ex;
        }

        return doc;
    }
}
Community
  • 1
  • 1
MikeB
  • 37
  • 2
  • 9
  • 2
    Can you show the start of each XML file - it looks to me like the second file refers to a DTD that can not be found on your machine ( C:\Projects\AreYouSmart\abch.dtd ) - Incidentally, I can't open the second file from my browser – DaveH Mar 25 '14 at 10:23
  • You could have a look at http://stackoverflow.com/questions/155101/make-documentbuilder-parse-ignore-dtd-references to see how you can ignore the DTD in the second file – DaveH Mar 25 '14 at 10:31
  • @DaveHowes I'll add to your comments that the reference to the DTD is on a002z020103.xml file (line two) I'll delete my answer as you found the problem first. – Pablo Francisco Pérez Hidalgo Mar 25 '14 at 10:46

2 Answers2

1

That XML file has the line:

<!DOCTYPE tabela_kursow SYSTEM "abch.dtd">

It is the abch.dtd file that it cannot find.

Try this:

objDocumentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
stepanian
  • 11,373
  • 8
  • 43
  • 63
1

By default, Xerces (the built-in XML parser in Java) will try to load an external DTD file even if the parser is non-validating.

Calling setValidating(false) has no effect since it is already non-validating by default to start with. You can turn off external DTD loading after constructing the factory:

objDocumentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", 
                                     false);
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79