XML Validation with Java

  • Thread starter Mathias Winklhofer
  • Start date
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
 
W

Wayne Rasmussen

IIRC, there already exists validators to do this. Why reinvent the wheel?
 
M

Mathias Winklhofer

I spent a long time in Google and several newsgroups and didn't find any other validators than the ones I described in my posting. None of those worked. Do you know other validators that work with the provided example?

Thanks
Mathias Winklhofer
IIRC, there already exists validators to do this. Why reinvent the wheel?
Mathias Winklhofer wrote:

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
 
A

Andrew Thompson

"Mathias Winklhofer"...
..I spent a long time in Google and several newsgroups and didn't find

It's a pity you did not find how to use your
friggin' delete key, Mathias.

To post 18Kb to this group just for a one-line
answer is idiotic.
 
M

Mathias Winklhofer

It's a pity you did not find how to use your
friggin' delete key, Mathias.

To post 18Kb to this group just for a one-line
answer is idiotic.

You're right, and I apologize for that...
 
A

Andrew Thompson

"Wayne Rasmussen" ...
After, perhaps 15 seconds of work I found:

After 4 seconds of work I had deleted the
17Kb of this message that did not need
repeating.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top