M
Mathias Winklhofer
Hi
For several days I've been trying to validate a very simple XML-Document
against an XML Schema - so far without success. I've tried several
approaches, using libraries like Xerces, JAXP and an Oracle-library, but
none of those worked the way for example XML Spy works.
Here is my Schema:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root"/>
</xsd:schema>
This is the (invalid) XML-File that references the schema: <?xml
version="1.0" encoding="UTF-8"?> <root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='test.xsd'> <a></a> </root>
This is my first try to validate the Document against the schema:
try { DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation",true);
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-che
cking",true); parser.parse("C:\\test.xml"); }
catch(Exception ex) { ex.printStackTrace(); }
This runs without problems, however, it doesn't report a validation-error.
My second try:
try { SAXParserFactory parserFct =
SAXParserFactory.newInstance(); parserFct.setValidating(true);
parserFct.setFeature("http://xml.org/sax/features/validation", true);
parserFct.setFeature("http://apache.org/xml/features/validation/schema",
true);
parserFct.setFeature("http://apache.org/xml/features/validation/schema-full-
checking",true); javax.xml.parsers.SAXParser parser =
parserFct.newSAXParser(); parser.parse("C:\\test.xml", new
MyHandlerBase()); } catch(Exception ex)
{ ex.printStackTrace(); }
This is the Errorhandler I used:
class MeinErrorHandler implements ErrorHandler{ boolean b = false;
public void warning(SAXParseException e) throws SAXException {
System.out.println("WARNUNG: " + e); b = true; } public void
error(SAXParseException e) throws SAXException {
System.out.println("FEHLER: " + e); b = true; } public void
fatalError(SAXParseException e) throws SAXException {
System.out.println("LEIDER, LEIDER, AUS: " + e); } public boolean
warninghappened(){ return b; }}
Again, no validation-error.
My third try:
InputSource is = null; DOMParser parser = new DOMParser();
Document doc = null; try {
parser.setFeature("http://xml.org/sax/features/validation",true);
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-che
cking",true);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNames
paceSchemaLocation", "C:\\test.xsd" ); is = new
InputSource(new FileReader("C:\\test.xml"));
parser.parse(is); } catch(Exception ex)
{ ex.printStackTrace(); }
Again, no validation-error.
My fourth try involved the oracle.xml.parser.schema and
oracle.xml.parser.v2-packages.
Here is the code:
public void process(String xsdURI, String xmlURI) throws Exception
{
XSDBuilder builder = new XSDBuilder();
URL url = createURL(xsdURI);
// Build XML Schema Object
XMLSchema schemadoc = (XMLSchema) builder.build(url);
DOMParser dp = new DOMParser();
url = createURL(xmlURI);
// Set Schema Object for Validation
dp.setXMLSchema(schemadoc);
dp.setValidationMode(XMLParser.SCHEMA_VALIDATION);
dp.setPreserveWhitespace(true);
dp.setErrorStream(System.out);
try
{
System.out.println("Parsing " + xmlURI);
dp.parse(url);
System.out.println("The input file <" + xmlURI + "> parsed
without errors");
}
catch (XMLParseException pe)
{
System.out.println("Parser Exception: " + pe.getMessage());
}
catch (Exception e)
{
System.out.println("NonParserException: " + e.getMessage());
}
}
This worked fine with the test.xml-Document, however, when I tried to
validate a more complex document which used keys and key-references, it
couldn't validate the Document although XML-Spy had no problem with it.
Am I missing something here? Does anybody know those problems and has a
solution for it, or perhaps a sample-code that works? Any help would be
greatly appreciated.
Thanks in advance
Mathias Winklhofer
For several days I've been trying to validate a very simple XML-Document
against an XML Schema - so far without success. I've tried several
approaches, using libraries like Xerces, JAXP and an Oracle-library, but
none of those worked the way for example XML Spy works.
Here is my Schema:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root"/>
</xsd:schema>
This is the (invalid) XML-File that references the schema: <?xml
version="1.0" encoding="UTF-8"?> <root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation='test.xsd'> <a></a> </root>
This is my first try to validate the Document against the schema:
try { DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation",true);
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-che
cking",true); parser.parse("C:\\test.xml"); }
catch(Exception ex) { ex.printStackTrace(); }
This runs without problems, however, it doesn't report a validation-error.
My second try:
try { SAXParserFactory parserFct =
SAXParserFactory.newInstance(); parserFct.setValidating(true);
parserFct.setFeature("http://xml.org/sax/features/validation", true);
parserFct.setFeature("http://apache.org/xml/features/validation/schema",
true);
parserFct.setFeature("http://apache.org/xml/features/validation/schema-full-
checking",true); javax.xml.parsers.SAXParser parser =
parserFct.newSAXParser(); parser.parse("C:\\test.xml", new
MyHandlerBase()); } catch(Exception ex)
{ ex.printStackTrace(); }
This is the Errorhandler I used:
class MeinErrorHandler implements ErrorHandler{ boolean b = false;
public void warning(SAXParseException e) throws SAXException {
System.out.println("WARNUNG: " + e); b = true; } public void
error(SAXParseException e) throws SAXException {
System.out.println("FEHLER: " + e); b = true; } public void
fatalError(SAXParseException e) throws SAXException {
System.out.println("LEIDER, LEIDER, AUS: " + e); } public boolean
warninghappened(){ return b; }}
Again, no validation-error.
My third try:
InputSource is = null; DOMParser parser = new DOMParser();
Document doc = null; try {
parser.setFeature("http://xml.org/sax/features/validation",true);
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-che
cking",true);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNames
paceSchemaLocation", "C:\\test.xsd" ); is = new
InputSource(new FileReader("C:\\test.xml"));
parser.parse(is); } catch(Exception ex)
{ ex.printStackTrace(); }
Again, no validation-error.
My fourth try involved the oracle.xml.parser.schema and
oracle.xml.parser.v2-packages.
Here is the code:
public void process(String xsdURI, String xmlURI) throws Exception
{
XSDBuilder builder = new XSDBuilder();
URL url = createURL(xsdURI);
// Build XML Schema Object
XMLSchema schemadoc = (XMLSchema) builder.build(url);
DOMParser dp = new DOMParser();
url = createURL(xmlURI);
// Set Schema Object for Validation
dp.setXMLSchema(schemadoc);
dp.setValidationMode(XMLParser.SCHEMA_VALIDATION);
dp.setPreserveWhitespace(true);
dp.setErrorStream(System.out);
try
{
System.out.println("Parsing " + xmlURI);
dp.parse(url);
System.out.println("The input file <" + xmlURI + "> parsed
without errors");
}
catch (XMLParseException pe)
{
System.out.println("Parser Exception: " + pe.getMessage());
}
catch (Exception e)
{
System.out.println("NonParserException: " + e.getMessage());
}
}
This worked fine with the test.xml-Document, however, when I tried to
validate a more complex document which used keys and key-references, it
couldn't validate the Document although XML-Spy had no problem with it.
Am I missing something here? Does anybody know those problems and has a
solution for it, or perhaps a sample-code that works? Any help would be
greatly appreciated.
Thanks in advance
Mathias Winklhofer