For a day or two now I've been trying to get XML parsing with XML Schema validation working using Xerces-J 2.7.1. Despite a sample document and schema that parse properly, I kept running into this in the test harness:
cvc-elt.1: Cannot find the declaration of element 'purchaseOrder'
Apparently that means "I didn't read about this tag in your schema." I fiddled some more and came up with this warning:
Failed to read schema document
which makes sense. What I couldn't figure out is why my test application which validated an XML file passed on the command line worked, and my application that parsed an InputStream failed.
It turns out that if you invoke XMLReader.parse(String systemId) instead of XMLReader.parse(InputSource input), it will use the systemId URL to figure out where the schema or DTD file may be. With a InputSource created from an InputStream, it doesn't have that URL and is left searching the current directory.
The solution is to use InputSource.setSystemId() to provide the input source with a URL that can used to help the schema/DTD search.
For those who are curious, below are the very simple files I'm using to make sure the validation works as it should.
I would like to syntax highlight my XML and Java code samples in this blog, but I can't see an easy way to do so in WordPress. I found a good XML and Java syntax coloring tool which uses SilverCity to parse code. However, without being able to specify some CSS the coloring won't work here.
First, we have a simple schema:
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:test"
targetNamespace="urn:test">
<xsd:element name="purchaseOrder">
<xsd:complexType>
<xsd:attribute name="partNum" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Next, we make a valid XML file for it:
<p:purchaseOrder xmlns:p="urn:test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:test basic.xsd"
partNum="1200" />
To make a well-formed but invalid XML file, just remove the "partnum" attribute from the above.
Tags: No Comments

0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.