XML Validation with Java

Discussion in 'Java' started by Mathias Winklhofer, Feb 1, 2004.

  1. 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
    Mathias Winklhofer, Feb 1, 2004
    #1
    1. Advertising

  2. 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
    Wayne Rasmussen, Feb 1, 2004
    #2
    1. Advertising

  3. 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" <> schrieb im Newsbeitrag news:...
    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
    Mathias Winklhofer, Feb 2, 2004
    #3
  4. "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.

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Feb 2, 2004
    #4
  5. > 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...
    Mathias Winklhofer, Feb 2, 2004
    #5
  6. 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" <>
    > schrieb im Newsbeitrag
    > news:...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

    >
    Wayne Rasmussen, Feb 2, 2004
    #6
  7. Andrew Thompson, Feb 3, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page