You are hereOverriding DTD files in Java
Overriding DTD files in Java
Speed increase
Some people know I'm working on an application called mac2date. For that application I need to parse XML files present on the Hard Drive. As every XML should do they declare their DTD (or schema for newer systems). But many of the files I read declare it using a path on the internet.
This means the XML parser I use needs to get the online-DTD every time it reads an XML file. That's something that's really really slow. Finally I found a way to override the declared DTD with a local one.
Difference? Speed-increase from 33 seconds to 2 seconds for parsing 100 XML files.
The code
The code:
// construct the documentBuilder (XML parser) and the XPath expressions documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); // override the DTD from the pList file and use a local one documentBuilder.setEntityResolver(new PListEntityResolver()); // add here the rest of your parsing code
And the needed class:
package org.mac2date;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
public class PListEntityResolver implements EntityResolver {
public InputSource resolveEntity (String publicId, String systemId){
return new InputSource("/System/Library/DTDs/PropertyList.dtd");
}
}
Tags





We should check the sources tho. Downloading right now those of Firefox...
The files
content/base/src/nsNameSpaceManager.cppandextensions/schema-validation/src/nsSchemaValidator.cpplook the most interesting.Just do a
grep -R w3.org * | lesson the sources of firefox.XML Catalogs are another way of achieving this. They can be better if you do not wish to have to change the code itself each time the location changes. Its basically a mapping file between the URL in the dtd/schema and a local copy.
See http://en.wikipedia.org/wiki/XML_Catalog for more details and a Java example