[jdom-interest] DTD locations when reading XML from a resource jar

Petri Wessman orava at iki.fi
Fri May 4 09:48:38 PDT 2001

Hello all, ran into a problem just now and I'm wondering if anyone has
ny helpful hints.

I have a servlet packaged into a jar (war) file, and the jar also
includes some XML configuration files which the servlet reads on
startup. To read the XML I use code essentially like the following:

InputStream in = getServletContext().getResourceAsStream(xmlfile);
SAXBuilder parser = new SAXBuilder(validate);
Document doc = parser.build(in);

This works fine without validation when the XML does not contain a
DOCTYPE statement.

However, if the document contains a DOCTYPE I get an error:

  JDOM XML parse error: Error on line 2: org.apache.crimson.parser/P-055 configuration.dtd

where line 2 of configuration.xml contains:

  <!DOCTYPE configuration SYSTEM "configuration.dtd">

Both the XML file and the DTD are in the same relative place in the
jar file.

Now, it's natural that the error is occurring, the parser has no way
of knowing where the DTD is located. I would have to tell JDOM (or
Crimson?) where to get the actual DTD from. Is there a neat solution
for this, or should I just forget about validation when loading XML
from jar files?

The appserver I'm using (JBoss) opens up the jar files into a
filesystem location on deployment, and it's quite possible for me to
construct a file path to the DTD by creatively using the servlet
context helper methods. In other words, I can give the parser the file
location of the DTD (since I suspect having the parser read from the
jar file might not be possible). However, I'm stumped as to how I
should tell the parser to fetch the DTD from a specified directory



More information about the jdom-interest mailing list