XML Validation with Java  
Author Message
Mathias Winklhofer





PostPosted: 2004-2-2 2:33:00 Top

java-programmer, XML Validation with Java 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


 
Wayne Rasmussen





PostPosted: 2004-2-2 0:52:00 Top

java-programmer >> XML Validation with Java

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

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

 
Mathias Winklhofer





PostPosted: 2004-2-2 19:52:00 Top

java-programmer >> XML Validation with Java This is a multi-part message in MIME format.


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
"Wayne Rasmussen" <email***@***.com> schrieb im Newsbeitrag news:email***@***.com...
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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1276" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>
<DIV><FONT face=Arial size=2>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?</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Thanks</FONT></DIV>
<DIV><FONT face=Arial size=2>Mathias Winklhofer</FONT></DIV></DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Wayne Rasmussen" <<A
href="mailto:email***@***.com">email***@***.com</A>>
schrieb im Newsbeitrag <A
href="news:email***@***.com">news:email***@***.com</A>...</DIV>IIRC,
there already exists validators to do this.  Why reinvent the wheel?
<P>Mathias Winklhofer wrote:
<BLOCKQUOTE TYPE="CITE">Hi
<P>For several days I've been trying to validate a very simple XML-Document
<BR>against an XML Schema - so far without success. I've tried several
<BR>approaches, using libraries like Xerces, JAXP and an Oracle-library, but
<BR>none of those worked the way for example XML Spy works.
<P>Here is my Schema:
<P><?xml version="1.0" encoding="UTF-8"?> <BR> <xsd:schema
xmlns:xsd="<A
href="http://www.w3.org/2001/XMLSchema">http://www.w3.org/2001/XMLSchema</A>">
<BR>        <xsd:element
name="root"/> <BR> </xsd:schema>
<P>This is the (invalid) XML-File that references the schema: <?xml
<BR>version="1.0" encoding="UTF-8"?> <root <BR>xmlns:xsi="<A
href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</A>"
<BR>xsi:noNamespaceSchemaLocation='test.xsd'> <a></a>
</root> <BR>This is my first try to validate the Document against the
schema:
<P>   try        
{            
DOMParser parser = new DOMParser(); <BR>parser.setFeature("<A
href="http://xml.org/sax/features/validation"
,true?>http://xml.org/sax/features/validation",true</A>);
<BR>parser.setFeature("<A
href="http://apache.org/xml/features/validation/schema"
,true?>http://apache.org/xml/features/validation/schema",true</A>);
<BR>parser.setFeature("<A
href="http://apache.org/xml/features/validation/schema-full-che">http://apache.org/xml/features/validation/schema-full-che</A>
<BR>cking",true);            
parser.parse("C:\\test.xml");        
} <BR>catch(Exception ex)        
{            
ex.printStackTrace();         }
<P>This runs without problems, however, it doesn't report a
validation-error.
<P>My second try:
<P>  try        
{            
SAXParserFactory parserFct =
<BR>SAXParserFactory.newInstance();             
parserFct.setValidating(true); <BR>parserFct.setFeature("<A
href="http://xml.org/sax/features/validation">http://xml.org/sax/features/validation</A>",
true); <BR>parserFct.setFeature("<A
href="http://apache.org/xml/features/validation/schema">http://apache.org/xml/features/validation/schema</A>",
<BR>true); <BR>parserFct.setFeature("<A
href="http://apache.org/xml/features/validation/schema-full">http://apache.org/xml/features/validation/schema-full</A>-
<BR>checking",true);            
javax.xml.parsers.SAXParser parser =
<BR>parserFct.newSAXParser();            
parser.parse("C:\\test.xml", new
<BR>MyHandlerBase());                 
}         catch(Exception ex)
<BR>{            
ex.printStackTrace();         }
<BR>This is the Errorhandler I used:
<P>  class MeinErrorHandler implements ErrorHandler{  
boolean b = false; <BR>public void warning(SAXParseException e) throws
SAXException    { <BR>System.out.println("WARNUNG: " +
e);      b = true;   }   public
void <BR>error(SAXParseException e) throws SAXException    {
<BR>System.out.println("FEHLER: " + e);     b =
true;   }   public void <BR>fatalError(SAXParseException
e) throws SAXException   { <BR>System.out.println("LEIDER, LEIDER,
AUS: " + e);   }   public boolean
<BR>warninghappened(){     return b;   }}
<P>Again, no validation-error.
<P>My third try:
<P>  InputSource is =
null;         DOMParser parser = new
DOMParser(); <BR>Document doc =
null;        
try          {
<BR>parser.setFeature("<A href="http://xml.org/sax/features/validation"
,true?>http://xml.org/sax/features/validation",true</A>);
<BR>parser.setFeature("<A
href="http://apache.org/xml/features/validation/schema"
,true?>http://apache.org/xml/features/validation/schema",true</A>);
<BR>parser.setFeature("<A
href="http://apache.org/xml/features/validation/schema-full-che">http://apache.org/xml/features/validation/schema-full-che</A>
<BR>cking",true); <BR>parser.setProperty("<A
href="http://apache.org/xml/properties/schema/external-noNames">http://apache.org/xml/properties/schema/external-noNames</A>
<BR>paceSchemaLocation", "C:\\test.xsd"
);              
is = new <BR>InputSource(new FileReader("C:\\test.xml"));
<BR>     
parser.parse(is);        
}         catch(Exception ex)
<BR>{             
ex.printStackTrace();         }
<P>Again, no validation-error.
<P>My fourth try involved the oracle.xml.parser.schema and
<BR>oracle.xml.parser.v2-packages. <BR>Here is the code:
<P>public void process(String xsdURI, String xmlURI) throws Exception
<BR>    { <BR>       
XSDBuilder builder = new XSDBuilder();
<BR>        URL url = createURL(xsdURI);
<BR>        // Build XML Schema Object
<BR>        XMLSchema schemadoc =
(XMLSchema) builder.build(url);
<BR>        DOMParser dp = new
DOMParser(); <BR>        url =
createURL(xmlURI); <BR>        // Set
Schema Object for Validation <BR>       
dp.setXMLSchema(schemadoc); <BR>       
dp.setValidationMode(XMLParser.SCHEMA_VALIDATION);
<BR>       
dp.setPreserveWhitespace(true);
<BR>       
dp.setErrorStream(System.out);
<BR>        try
<BR>        {
<BR>           
System.out.println("Parsing " + xmlURI);
<BR>           
dp.parse(url);
<BR>           
System.out.println("The input file <" + xmlURI + "> parsed <BR>without
errors"); <BR>        }
<BR>        catch (XMLParseException pe)
<BR>        {
<BR>           
System.out.println("Parser Exception: " + pe.getMessage());
<BR>        }
<BR>        catch (Exception e)
<BR>        {
<BR>           
System.out.println("NonParserException: " + e.getMessage());
<BR>        } <BR>    }
<P>This worked fine with the test.xml-Document, however, when I tried to
<BR>validate a more complex document which used keys and key-references, it
<BR>couldn't validate the Document although XML-Spy had no problem with it.
<P>Am I missing something here? Does anybody know those problems and has a
<BR>solution for it, or perhaps a sample-code that works? Any help would be
<BR>greatly appreciated.
<P>Thanks in advance <BR>Mathias
Winklhofer</P></BLOCKQUOTE></BLOCKQUOTE></BODY></HTML>

 
 
Andrew Thompson





PostPosted: 2004-2-2 20:09:00 Top

java-programmer >> XML Validation with Java "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.


 
 
Mathias Winklhofer





PostPosted: 2004-2-2 20:20:00 Top

java-programmer >> XML Validation with Java > 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...


 
 
Wayne Rasmussen





PostPosted: 2004-2-2 20:28:00 Top

java-programmer >> XML Validation with Java

After, perhaps 15 seconds of work I found:

http://xml.apache.org/xerces-c/index.html
http://www.w3.org/XML/

Mathias Winklhofer wrote:

> 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? ThanksMathias Winklhofer
>
> "Wayne Rasmussen" <email***@***.com>
> schrieb im Newsbeitrag
> news:email***@***.com...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.setFe
> > ture("http://xml.org/sax/features/validation",true);
> > pa
> > ser.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);
> > parser
> > ct.setFeature("http://apache.org/xml/features/validation/schema",
> >
> > true);
> > parser
> > ct.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.setPro
> > erty("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
>

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<body bgcolor="#FFFFFF">
After, perhaps 15 seconds of work I found:
<p><A HREF="http://xml.apache.org/xerces-c/index.html">http://xml.apache.org/xerces-c/index.html</A>
<br><A HREF="http://www.w3.org/XML/">http://www.w3.org/XML/</A>
<p>Mathias Winklhofer wrote:
<blockquote TYPE=CITE><style></style>
<font face="Arial"><font size=-1>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?</font></font> <font face="Arial"><font size=-1>Thanks</font></font><font face="Arial"><font size=-1>Mathias
Winklhofer</font></font>
<blockquote dir=ltr
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">"Wayne
Rasmussen" <<a href="mailto:email***@***.com">email***@***.com</a>>
schrieb im Newsbeitrag <a href="news:email***@***.com">news:email***@***.com</a>...IIRC,
there already exists validators to do this.  Why reinvent the wheel?
<p>Mathias Winklhofer wrote:
<blockquote TYPE="CITE">Hi
<p>For several days I've been trying to validate a very simple XML-Document
<br>against an XML Schema - so far without success. I've tried several
<br>approaches, using libraries like Xerces, JAXP and an Oracle-library,
but
<br>none of those worked the way for example XML Spy works.
<p>Here is my Schema:
<p><?xml version="1.0" encoding="UTF-8"?>
<br> <xsd:schema xmlns:xsd="<a href="http://www.w3.org/2001/XMLSchema">http://www.w3.org/2001/XMLSchema</a>">
<br>        <xsd:element name="root"/>
<br> </xsd:schema>
<p>This is the (invalid) XML-File that references the schema: <?xml
<br>version="1.0" encoding="UTF-8"?> <root
<br>xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>"
<br>xsi:noNamespaceSchemaLocation='test.xsd'> <a></a> </root>
<br>This is my first try to validate the Document against the schema:
<p>   try         {            
DOMParser parser = new DOMParser();
<br>parser.setFeature("<a href="http://xml.org/sax/features/validation" ,true?>http://xml.org/sax/features/validation",true</a>);
<br>parser.setFeature("<a href="http://apache.org/xml/features/validation/schema" ,true?>http://apache.org/xml/features/validation/schema",true</a>);
<br>parser.setFeature("<a href="http://apache.org/xml/features/validation/schema-full-che">http://apache.org/xml/features/validation/schema-full-che</a>
<br>cking",true);            
parser.parse("C:\\test.xml");        
}
<br>catch(Exception ex)        
{            
ex.printStackTrace();         }
<p>This runs without problems, however, it doesn't report a validation-error.
<p>My second try:
<p>  try         {            
SAXParserFactory parserFct =
<br>SAXParserFactory.newInstance();             
parserFct.setValidating(true);
<br>parserFct.setFeature("<a href="http://xml.org/sax/features/validation">http://xml.org/sax/features/validation</a>",
true);
<br>parserFct.setFeature("<a href="http://apache.org/xml/features/validation/schema">http://apache.org/xml/features/validation/schema</a>",
<br>true);
<br>parserFct.setFeature("<a href="http://apache.org/xml/features/validation/schema-full">http://apache.org/xml/features/validation/schema-full</a>-
<br>checking",true);            
javax.xml.parsers.SAXParser parser =
<br>parserFct.newSAXParser();            
parser.parse("C:\\test.xml", new
<br>MyHandlerBase());                 
}         catch(Exception ex)
<br>{            
ex.printStackTrace();         }
<br>This is the Errorhandler I used:
<p>  class MeinErrorHandler implements ErrorHandler{   boolean
b = false;
<br>public void warning(SAXParseException e) throws SAXException   
{
<br>System.out.println("WARNUNG: " + e);     
b = true;   }   public void
<br>error(SAXParseException e) throws SAXException    {
<br>System.out.println("FEHLER: " + e);     b = true;  
}   public void
<br>fatalError(SAXParseException e) throws SAXException   {
<br>System.out.println("LEIDER, LEIDER, AUS: " + e);   }  
public boolean
<br>warninghappened(){     return b;   }}
<p>Again, no validation-error.
<p>My third try:
<p>  InputSource is = null;        
DOMParser parser = new DOMParser();
<br>Document doc = null;        
try          {
<br>parser.setFeature("<a href="http://xml.org/sax/features/validation" ,true?>http://xml.org/sax/features/validation",true</a>);
<br>parser.setFeature("<a href="http://apache.org/xml/features/validation/schema" ,true?>http://apache.org/xml/features/validation/schema",true</a>);
<br>parser.setFeature("<a href="http://apache.org/xml/features/validation/schema-full-che">http://apache.org/xml/features/validation/schema-full-che</a>
<br>cking",true);
<br>parser.setProperty("<a href="http://apache.org/xml/properties/schema/external-noNames">http://apache.org/xml/properties/schema/external-noNames</a>
<br>paceSchemaLocation", "C:\\test.xsd" );              
is = new
<br>InputSource(new FileReader("C:\\test.xml"));
<br>      parser.parse(is);        
}         catch(Exception ex)
<br>{             
ex.printStackTrace();         }
<p>Again, no validation-error.
<p>My fourth try involved the oracle.xml.parser.schema and
<br>oracle.xml.parser.v2-packages.
<br>Here is the code:
<p>public void process(String xsdURI, String xmlURI) throws Exception
<br>    {
<br>        XSDBuilder builder = new
XSDBuilder();
<br>        URL url = createURL(xsdURI);
<br>        // Build XML Schema Object
<br>        XMLSchema schemadoc = (XMLSchema)
builder.build(url);
<br>        DOMParser dp = new DOMParser();
<br>        url = createURL(xmlURI);
<br>        // Set Schema Object for
Validation
<br>        dp.setXMLSchema(schemadoc);
<br>        dp.setValidationMode(XMLParser.SCHEMA_VALIDATION);
<br>        dp.setPreserveWhitespace(true);
<br>        dp.setErrorStream(System.out);
<br>        try
<br>        {
<br>           
System.out.println("Parsing " + xmlURI);
<br>           
dp.parse(url);
<br>           
System.out.println("The input file <" + xmlURI + "> parsed
<br>without errors");
<br>        }
<br>        catch (XMLParseException
pe)
<br>        {
<br>           
System.out.println("Parser Exception: " + pe.getMessage());
<br>        }
<br>        catch (Exception e)
<br>        {
<br>           
System.out.println("NonParserException: " + e.getMessage());
<br>        }
<br>    }
<p>This worked fine with the test.xml-Document, however, when I tried to
<br>validate a more complex document which used keys and key-references,
it
<br>couldn't validate the Document although XML-Spy had no problem with
it.
<p>Am I missing something here? Does anybody know those problems and has
a
<br>solution for it, or perhaps a sample-code that works? Any help would
be
<br>greatly appreciated.
<p>Thanks in advance
<br>Mathias Winklhofer</blockquote>
</blockquote>
</blockquote>

</body>
</html>

 
 
Andrew Thompson





PostPosted: 2004-2-3 12:02:00 Top

java-programmer >> XML Validation with Java "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.