Java SAX Parser raises UnknownHostException
Asked Answered
L

4

7

The XML file I want to parse starts with :

<!DOCTYPE plist PUBLIC "-//...//DTD PLIST 1.0//EN" "http://www.....dtd">

So when I start the SAX praser, it tries to access this DTD online, and I get a java.net.UnknownHostException.

  1. I cannot modify the XML file before feeding it to the SAX parser
  2. I have to run even with no internet connection

How can I change the SAX Parser behaviour so that it does not try to load the DTD ? Thanks.

javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setValidating(false);

javax.xml.parsers.SAXParser parser = factory.newSAXParser();
parser.parse(xmlFile, handler);
Lemke answered 17/11, 2008 at 15:0 Comment(0)
F
11

Ok, turns out the parse() method overrides any previously set entity resolvers with the handler passed in to the parse method. The following code should work:

javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
factory.setValidating(false);
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
parser.parse(new java.io.File("x.xml"), new org.xml.sax.helpers.DefaultHandler(){
        public org.xml.sax.InputSource resolveEntity(String publicId, String systemId)
                throws org.xml.sax.SAXException, java.io.IOException {
            System.out.println("Ignoring: " + publicId + ", " + systemId);
            return new org.xml.sax.InputSource(new java.io.StringReader(""));
        }
    }); 
Freely answered 17/11, 2008 at 16:10 Comment(0)
A
1

Use XMLReader instead of SAXParser.

    XMLReader reader =XMLReaderFactory.createXMLReader();
 reader.setEntityResolver(new DummyEntityResolver());
    reader.setContentHandler(handler);
 reader.parse(inputSource);

It should also work with SAXParser, but for some reasons it doesn't.

Azotic answered 10/10, 2011 at 13:4 Comment(0)
F
0

You can implement a custom EntityResolver which is what is used to lookup external entities during XML parsing.

org.xml.sax.EntityResolver customEntityResolver = new DummyEntityResolver();
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
parser.getXMLReader().setEntityResolver(customEntityResolver);
parser.parse(xmlFile, handler);

And in your custom EntityResolver, just always return null. I think that should fix this problem.

Freely answered 17/11, 2008 at 15:20 Comment(0)
C
0

You should provide an EntityResolve to have the problem resolve. I will recommend you to write a resolver that will know how to read the DTDs locally instead (provided that you have them shipped together with your application). Otherwise, return null like Gowri suggested.

You might want to read up the the API doc.

yc

Caesarism answered 17/11, 2008 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.