Java XML Validation Problem

Discussion in 'XML' started by Brian, Jul 31, 2004.

  1. Brian

    Brian Guest

    I am having alot of trouble getting a XML document validated with a
    schema.

    I got a sample document and schema off of w3schools.com, which passed
    an online xml validator:
    http://tools.decisionsoft.com/schemaValidate.html. I cannot, however,
    get them validated programmatically.

    The documents are:

    node.xsd:

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3schools.com"
    xmlns="http://www.w3schools.com"
    elementFormDefault="qualified"><xs:element name="note">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="to" type="xs:string"/>
    <xs:element name="from" type="xs:string"/>
    <xs:element name="heading" type="xs:string"/>
    <xs:element name="body" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element></xs:schema>

    node.xml:

    <?xml version="1.0"?>
    <note
    xmlns="http://www.w3schools.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.w3schools.com note.xsd">

    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>

    I'm using java 1.5 currently, although I had problems with 1.4 and
    xerces, so I'm pretty confident the problem isn't related to the java
    version.

    My code is:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    SchemaFactory sFact =
    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    factory.setSchema(sFact.newSchema(new java.io.File(SCHEMA_FILE)));

    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new ErrorHandler() {
    public void warning (SAXParseException exception) throws SAXException
    {
    System.err.println(exception);
    }
    public void error (SAXParseException exception) throws SAXException {
    System.err.println(exception);
    }
    public void fatalError (SAXParseException exception) throws
    SAXException {
    System.err.println(exception);
    }
    });

    org.w3c.dom.Document doc = builder.parse(file);


    The error I recieve is:

    org.xml.sax.SAXParseException:
    http://www.w3.org/TR/xml-schema-1#cvc-elt.1?note
    org.xml.sax.SAXParseException:
    http://www.w3.org/TR/xml-schema-1#cvc-elt.1?note
    Exception in thread "main" java.lang.IllegalStateException
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl$3.checkState(ValidatorHandlerImpl.java:411)
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl$3.getElementTypeInfo(ValidatorHandlerImpl.java:441)
    at com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.elementAug(JAXPValidatorComponent.java:299)
    at com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent$SAX2XNI.endElement(JAXPValidatorComponent.java:291)
    at com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(XNI2SAX.java:163)
    at com.sun.org.apache.xerces.internal.jaxp.validation.XNI2SAXEx.endElement(XNI2SAXEx.java:108)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.endElement(XMLSchemaValidator.java:818)
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.endElement(ValidatorHandlerImpl.java:339)
    at com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.endElement(XNI2SAX.java:163)
    at com.sun.org.apache.xerces.internal.jaxp.JAXPValidatorComponent.endElement(JAXPValidatorComponent.java:206)
    at com.sun.org.apache.xerces.internal.xinclude.XIncludeHandler.endElement(XIncludeHandler.java:790)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1241)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1685)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:248)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:169)
    ....

    I am behind a web proxy, and I don't know if the validation process
    involves retrieving files at the URLs in the xml and xsd files, which
    could be the problem.

    can someone shed some light on this?

    Thanks in advance.
    Brian, Jul 31, 2004
    #1
    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