472,961 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

Java XML Validation Problem

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
0 4064

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Dave W | last post by:
I wish to validate a string but i'm not quite sure how to go about doing it. I'd like to ensure that it is 9 characters long and in the following format. "LL 00 LLL" - Thats letter, letter,...
1
by: Ulf Nordlund | last post by:
I am using an XML schema to check (validate) the structure of an XML document. But since I cannot check everything I need using a schema, I am thinking of using XSL for more detailed checks (value...
4
by: Marcin Cenkier | last post by:
Hi, I can create a schema from xsd file: Schema s = SchemaFactory.newInstance(_XMLConstants.W3C_XML_SCHEMA_NS_URI_).newSchema(new StreamSource(res.getInputStream())); but when using...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
4
by: Francois Stander | last post by:
Hi, hope someone can help me. It seems imposible to read data from a server, however, I can read the validation data from the server and hold it in dataviews . datasets or data tables in my asp...
1
by: ahsan19 | last post by:
Our company is doing a project for the City of Chicago and we are trying to communicate with their servers using web services. Their environment is java based but we are using .NET on our end. The...
1
by: jaimemartin | last post by:
hello, I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in...
2
by: poornimanithya76 | last post by:
Hi, I am having grid view an my page which is having template fields(text box, check box and drop down).If user clicks on check box i need to consider text box and drop down as a required fields.i...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.