By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,119 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,119 IT Pros & Developers. It's quick & easy.

Java XML Validation Problem

P: n/a
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_SCH EMA_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(ValidatorHandle rImpl.java:411)
at com.sun.org.apache.xerces.internal.jaxp.validation .ValidatorHandlerImpl$3.getElementTypeInfo(Validat orHandlerImpl.java:441)
at com.sun.org.apache.xerces.internal.jaxp.JAXPValida torComponent$SAX2XNI.elementAug(JAXPValidatorCompo nent.java:299)
at com.sun.org.apache.xerces.internal.jaxp.JAXPValida torComponent$SAX2XNI.endElement(JAXPValidatorCompo nent.java:291)
at com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.en dElement(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.XMLSche maValidator.endElement(XMLSchemaValidator.java:818 )
at com.sun.org.apache.xerces.internal.jaxp.validation .ValidatorHandlerImpl.endElement(ValidatorHandlerI mpl.java:339)
at com.sun.org.apache.xerces.internal.jaxp.XNI2SAX.en dElement(XNI2SAX.java:163)
at com.sun.org.apache.xerces.internal.jaxp.JAXPValida torComponent.endElement(JAXPValidatorComponent.jav a:206)
at com.sun.org.apache.xerces.internal.xinclude.XInclu deHandler.endElement(XIncludeHandler.java:790)
at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl.scanEndElement(XMLDocumentFra gmentScannerImpl.java:1241)
at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl$FragmentContentDispatcher.dis patch(XMLDocumentFragmentScannerImpl.java:1685)
at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl.scanDocument(XMLDocumentFragm entScannerImpl.java:368)
at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLPars er.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.DOMPars er.parse(DOMParser.java:248)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBu ilderImpl.parse(DocumentBuilderImpl.java:292)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBu ilder.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.
Jul 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.