[jdom-interest] SAXBuilder with EntityResolver, XML Schemas,
DOCTYPE and namespaces
David Patterson
dpatterson at i-a-i.com
Tue Oct 4 13:01:46 PDT 2005
I had a problem trying to use the SAXBuilder to process an XML file that
uses namespaces, and has XML Schema validation and uses an EntityResolver to
locate the schema files. The XML file also has a DOCTYPE
declaration for an entity. (I mention all of these in case one is known to
cause problems.)
I've solved the problem (perhaps not in the best way) but wanted to bring it
to your attention in case it is a known or unknown bug.
I tried using the build( File), build( InputStream) and build( InputSource),
and all fail similarly. I have tested the correct solution on the File and
InputStream versions and they both work the same.
This is on the root element of my XML file.
The XML file looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dmodule [
<!NOTATION cgm SYSTEM "explorer.exe">
<!ENTITY ICN-AE-A-000000-0-U8025-00536-A-01-1
SYSTEM
"file:///c:/longfilename.cgm" NDATA cgm>
]>
<dmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="descriptSchema.xsd"
id="DMC-BICYCLEXXXAAA-AAA-D00-00-00-0000-041A-A_000-03.xml">
<idstatus>
.
</dmodule>
My code is:
SAXBuilder saxb = new SAXBuilder(
"org.apache.xerces.parsers.SAXParser", true);
DomEntityResolver der = new DomEntityResolver(
schemaDm, DomEntityResolver.VERBOSE );
// the next entry does not change the result
// true fails in the same way as false
saxb.setExpandEntities( false );
saxb.setFeature( "http://apache.org/xml/features/validation/schema",
true);
saxb.setProperty( JAXPConstants.JAXP_SCHEMA_LANGUAGE,
JAXPConstants.W3C_XML_SCHEMA );
saxb.setFeature( "http://xml.org/sax/features/namespaces", true );
saxb.setEntityResolver( der );
try
{
doc = saxb.build( file );
}
catch (JDOMException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
Note: With the schema validation feature set to true and the setProperty
commented out, the EntityResolver is called at construction time, and for
each schema file, but each element is flagged as needing to be declared. The
text of the traceback is listed below. Note that it includes references to
an XMLDTDValidator object.
With the schema validation feature commented out, and the setProperty in
place, the EntityResolver is not called, and there is no validation
performed. (I added invalid content to one of the files and there were no
errors listed.)
With both the setFeature and the setProperty in place, however, I get the
calls from the EntityResolver indicating that it is processing the schema
files, and for the good files, I get no errror, but for the bad file, I get
an error indicating a bad validation.
In my DomEntityResolver, I'm getting a message from the constructor, but no
messages from the calls to resolveEntity().
In the traceback for the exception I'm troubled by seeing the
dtd.XMLDTDValidator.
Caused by: org.xml.sax.SAXParseException: Element type "dmodule" must be
declared.
at
org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown
Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown
Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown
Source)
at
org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown
Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown
Source)
at
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown
Source)
at
org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.
scanRootElementHook(Unknown Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatc
her.
dispatch(Unknown Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:453)
My first bypass was to create an org.w3c.dom.Document and then use a
DOMBuilder to convert the org.w3c.dom.Document into an org.jdom.Document.
David R. Patterson
Senior Software Engineer
<mailto:dpatterson at i-a-i.com> dpatterson at i-a-i.com
Intelligent Automation, Inc.
(301) 294-4632
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20051004/e98e069d/attachment.htm
More information about the jdom-interest
mailing list