Friday, June 1, 2012

XML Parsers Getting DTDs Over Internet

One common issue with web development particularly is that for XML config files, some parser will go through the Internet to download the DTD. This means the server can't be started when it's not able to reach the server hosting the DTD, for whatever reason.

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

In the above example, the server will go download the DTD from the URL above.

The solution for this is to download a local copy and specify it in the XML file. There is no need to change "PUBLIC" to "SYSTEM", just change the location.

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"dtd/hibernate-configuration-3.0.dtd">

In this example, the parser (Xerces) searched the relative path according to the directory where the Tomcat server was launched e.g. (/opt/tomcat/bin/), so all I needed to do was to put a copy of the DTD file in /opt/tomcat/bin/dtd. It's not an ideal solution. I would have preferred if the parser was smart enough to look in the classpath first, but it didn't, so I'm stuck with this solution.

No comments:

Post a Comment