[
Lists Home |
Date Index |
Thread Index
]
Hi,
As I am new to the world of XML and Java, I was exploring how I could
associate an XML document (whose name is passed in via the command line)
with an XML schema for validation purposes using the related Java DOM API.
The relevant Java code is as follows:
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
//builder.setErrorHandler(new MyErrorHandler());
document = builder.parse(new File(args[0]));
} catch (SAXException sxe) {
...
I am using the following simple XML document (test.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///C:/j2sdk1.4.2/practice/test.xsd">2003
-02-06</shiporder>
The corresponding XML schema (test.xsd) is as follows:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder" type="xs:date"/>
</xs:schema>
When I ran the Java program, I got the following errors:
Error: URI=file:C:/j2sdk1.4.2/practice/test.xml Line=3: Element type
"shiporder"
is not declared.
Error: URI=file:C:/j2sdk1.4.2/practice/test.xml Line=4: Attribute
"xmlns:xsi" is
not declared for element "shiporder".
Error: URI=file:C:/j2sdk1.4.2/practice/test.xml Line=4: Attribute
"xsi:noNamespa
ceSchemaLocation" is not declared for element "shiporder".
It seems that somehow the schema was not used for validation. Since I was
using the "xsi:noNamespaceSchemaLocation" attribute in my XML document, I
had actually started out by omitting the path to test.xsd (as this was in
the same location as the XML document). The same errors were also thrown in
this scenario.
Would appreciate any input on where I might have gone wrong. Thanks.
Peter
|