Wednesday, March 11, 2009

Ignoring loading external DTD while reading/parsing XML files

I encountered this problem while using xalan-J for XPath queries. We were using default xerces xml parser implementation. Our XML files referred to a DTD which we didn't have access to.

There are a number of workarounds for this. Choose the best that fits your situation.
  1. Remove the DTD declaration from XML file.
  2. Create an empty DTD file and turn off DTD validation.
  3. Turn off downloading of external DTDs and validation if the XML reader/parser implementation supports it. To do this for xerces, we did following:

    DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
    // Ignore loading external DTD
    dfactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    // turn-off validation which will require DTD to be present
    dfactory.setFeature(”http://xml.org/sax/features/validation”, false);
Somehow above code fragment does not wrap-around nicely when viewed in FF3. If anybody knows how to fix, let me know.

Thanks to Denish for the 3rd workaround.