views:

2966

answers:

3

I got dtd in file and I cant remove it. When i try to parse it in Java I get "Caused by: java.net.SocketException: Network is unreachable: connect", because its remote dtd. can I disable somehow dtd checking?

+10  A: 

You should be able to specify your own EntityResolver, or use specific features of your parser? See here for some approaches.

A more complete example:

<?xml version="1.0"?>
<!DOCTYPE foo PUBLIC "//FOO//" "foo.dtd">
<foo>
    <bar>Value</bar>
</foo>

And xpath usage:

import java.io.File;
import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        builder.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                System.out.println("Ignoring " + publicId + ", " + systemId);
                return new InputSource(new StringReader(""));
            }
        });
        Document document = builder.parse(new File("src/foo.xml"));
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        String content = xpath.evaluate("/foo/bar/text()", document
                .getDocumentElement());
        System.out.println(content);
    }
}

Hope this helps...

toolkit
Thanks, this reduced some unit testing I was doing by a factor of 7.
Scott Markwell
+1  A: 

I had this problem before. I solved it by downloading and storing a local copy of the DTD and then validating against the local copy. You need to edit the XML file to point to the local copy.

<!DOCTYPE root-element SYSTEM "filename">

Little more info here: http://www.w3schools.com/dtd/dtd_intro.asp

I think you can also manually set some sort of validateOnParse property to "false" in your parser. Depends on what library you're using to parse the XML.

More info here: http://www.w3schools.com/dtd/dtd_validation.asp

Owen
+1  A: 

This worked for me:

 SAXParserFactory saxfac = SAXParserFactory.newInstance();
  saxfac.setValidating(false);
  try {
    saxfac.setFeature("http://xml.org/sax/features/validation", false);
    saxfac.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    saxfac.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    saxfac.setFeature("http://xml.org/sax/features/external-general-entities", false);
    saxfac.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
  }
  catch (Exception e1) {
    e1.printStackTrace();
  }
David
+1 - This is the best answer here - however it is enough to use this line only: ``saxfac.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);``
denis_k