users@jaxb.java.net

Re: SAXParseException: Why?

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Wed, 30 Apr 2003 11:19:44 -0700

> The message itself is clear enough, but if JAXB doesn't use dtd files,
> why is failure to find one treated as a fatal error?

Because that's what XML 1.0 says. It is a bug if we just let it go.

First of all, I think you should think about fixing your old in-house
application. If it sends out XML with a DOCTYPE declaration that doesn't
resolve, then the XML produced from your in-house application is not a
well-formed document. To me, fixing that is the right fix.


If you have a DTD, then you can set the system Id of the input document
and you can use your custom EntityResolver to make sure the DTD resolves
to your local copy. Everything will work nicely, though this is bit
complicated.


If you don't like neither, then you have to modify your JAXB
application to tolerate non well-formed documents. I think the easiest
way is to rely on a specific XML parser implementation that provides
this kind of option.

For example, Xerces allows you to do that through a property called
"http://apache.org/xml/features/nonvalidating/load-external-dtd"
(see http://xml.apache.org/xerces2-j/features.html)

So when you unmarshal a document, you need to manually build a XML
parser from Xerces, then pass it as a SAXSource object (along with your
InputSource) to the JAXB unmarshaller.

    unmarshaller.unmarshal(
        new SAXSource( configuredXmlParser, inputSource ) );

You can still validate your input document by using JAXB's unmarshaller,
if you do unmarshaller.setValidating(true).


regards,
--
Kohsuke KAWAGUCHI                  408-276-7063 (x17063)
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com