I'm trying to get Xerces version 1.4.3 (I've tried earlier version as well)
to validate some XML against a schema, but I can't get it to work.
Essentially, I'm trying to test failure and see what happens, but I can't
get it to fail. I've tried looking through the archives, but I couldn't find
anything that showed me anything different. Here's my code:
public void validate()
{
try
{
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation",
true);
parser.setFeature("http://xml.org/sax/features/namespaces",
true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true
);
parser.setFeature(
"http://apache.org/xml/features/validation/dynamic", true);
XMLHandler handler = new XMLHandler();
parser.setErrorHandler(handler);
FileReader fr = new FileReader("C:\\xml\\MyXML.xml");
parser.parse(new InputSource(fr));
}
catch(Exception e)
{
System.out.println(e.getClass().getName() + ": " +
e.getMessage());
}
}
The class XMLHandler is just simple implementation of the ErrorHandler
interface that just outputs the Exception messages to std_out.
Here's my XML and XML Schemas:
MyXML.xml -
<?xml version="1.0" encoding="UTF-8"?>
<MyXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\xml\MyXML.xsd">
<MyName>Nathanr</MyName>
</MyXML>
MyXML.xsd -
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
elementFormDefault="qualified">
<xsd:element name="MyXML">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MyName" type="xsd:string"
minOccurs="1" maxOccurs="1"/>
<xsd:element name="AssociateNumber"
type="xsd:short" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
This should fail because <AssociateNumber/> is required, but I can't get it
to do anything. The validate method above executes perfectly. How do I
catch/handle validation errors?
Thanks...
-Nathan Beyer
CONFIDENTIALITY NOTICE
This message and any included attachments are from Cerner Corporation and are
intended only for the addressee. The information contained in this message is
confidential and may constitute inside or non-public information under
international, federal, or state securities laws and is intended only for the
use of the addressee. Unauthorized forwarding, printing, copying,
distributing, or using such information is strictly prohibited and may be
unlawful. If you are not the addressee, please promptly delete this message
and notify the sender of the delivery error by e-mail or you may call Cerner�s
corporate offices in Kansas City, Missouri, U.S.A at (+1) (816) 221-1024.
------------------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]