473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 4087

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.