Java 5 to Java 6

Hi,

Can someone please help me here. I have the following class to validate an xml file against a schema. Basically it will work for java 5 but not 6.

import java.io.File;

import java.io.IOException;

import javax.xml.XMLConstants;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.dom.DOMSource;

import javax.xml.validation.Schema;

import javax.xml.validation.SchemaFactory;

import javax.xml.validation.Validator;

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

publicclass Test{

publicstaticvoid main(String[] aArgs)throws IOException, ParserConfigurationException{

try{

File lSchema =new File("schema.xsd");

File lXml =new File("xml.xml");

SchemaFactory lSchemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );

Schema schemaXSD = lSchemaFactory.newSchema(lSchema);

Validator lValidator = schemaXSD.newValidator();

DocumentBuilder lBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document lDocument = lBuilder.parse(lXml);

lValidator.validate(new DOMSource(lDocument));

System.out.println("Validation successful");

}catch (SAXException e){

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

if I use the following schema:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema

targetNamespace="http://www.test.com/settings"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

xmlns:tst="http://www.test.com/settings">

<xs:element name="name">

<xs:complexType>

<xs:sequence>

<xs:element name="first" type="xs:string"/>

<xs:element name="second" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

and the following xml:

<?xml version="1.0" encoding="UTF-8"?>

<tst:name

xmlns:tst="http://www.test.com/settings"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<first>Stephen</first>

<second>R</second>

</tst:name>

I get validation succesful under java 5 but under java 6 i get:

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element'tst:name'.

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)

at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1887)

at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:685)

at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:273)

at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:240)

at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:186)

at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:100)

at javax.xml.validation.Validator.validate(Validator.java:127)

at Test.main(Test.java:33)

Is anyone aware of any reason this would happen? Thank you in advance

[5311 byte] By [megasteoa] at [2007-11-27 8:09:37]
# 1

> Hi,

> Can someone please help me here. I have the following

> class to validate an xml file against a schema.

> Basically it will work for java 5 but not 6.

>

> import java.io.File;

> import java.io.IOException;

>

> import javax.xml.XMLConstants;

> import javax.xml.parsers.DocumentBuilder;

> import javax.xml.parsers.DocumentBuilderFactory;

> import

> javax.xml.parsers.ParserConfigurationException;

> import javax.xml.transform.dom.DOMSource;

> import javax.xml.validation.Schema;

> import javax.xml.validation.SchemaFactory;

> import javax.xml.validation.Validator;

>

> import org.w3c.dom.Document;

> import org.xml.sax.SAXException;

>

>

> public class Test {

>

>

> public static void main(String[] aArgs) throws

> s IOException, ParserConfigurationException{

>

> try {

> File lSchema = new File("schema.xsd");

> File lXml = new File("xml.xml");

>

> SchemaFactory lSchemaFactory =

> y = SchemaFactory.newInstance(

> XMLConstants.W3C_XML_SCHEMA_NS_URI );

> Schema schemaXSD =

> D = lSchemaFactory.newSchema(lSchema);

> Validator lValidator = schemaXSD.newValidator();

>

> DocumentBuilder lBuilder =

> r =

> DocumentBuilderFactory.newInstance().newDocumentBuilde

> r();

> Document lDocument = lBuilder.parse(lXml);

>

> lValidator.validate(new DOMSource(lDocument));

>

> System.out.println("Validation successful");

> } catch (SAXException e) {

> // TODO Auto-generated catch block

> e.printStackTrace();

> }

> }

> }

>

> if I use the following schema:

>

> > <?xml version="1.0" encoding="UTF-8"?>

> <xs:schema

>targetNamespace="http://www.test.com/settings"

> xmlns:xs="http://www.w3.org/2001/XMLSchema"

>xmlns:tst="http://www.test.com/settings">

> <xs:element name="name">

><xs:complexType>

><xs:sequence>

> <xs:element name="first" type="xs:string"/>

> <xs:element name="second" type="xs:string"/>

></xs:sequence>

></xs:complexType>

> </xs:element>

>

> </xs:schema>

>

> and the following xml:

>

> <?xml version="1.0" encoding="UTF-8"?>

> <tst:name

> xmlns:tst="http://www.test.com/settings"

> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

> >

>

> <first>Stephen</first>

> <second>R</second>

>

> </tst:name>

>

> I get validation succesful under java 5 but under

> java 6 i get:

>

> org.xml.sax.SAXParseException: cvc-elt.1:

> Cannot find the declaration of element 'tst:name'.

> at

> t

> com.sun.org.apache.xerces.internal.util.ErrorHandlerWr

> apper.createSAXParseException(ErrorHandlerWrapper.java

> :195)

> at

> t

> com.sun.org.apache.xerces.internal.util.ErrorHandlerWr

> apper.error(ErrorHandlerWrapper.java:131)

> at

> t

> com.sun.org.apache.xerces.internal.impl.XMLErrorReport

> er.reportError(XMLErrorReporter.java:384)

> at

> t

> com.sun.org.apache.xerces.internal.impl.XMLErrorReport

> er.reportError(XMLErrorReporter.java:318)

> at

> t

> com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaVa

> lidator.handleStartElement(XMLSchemaValidator.java:188

> 7)

> at

> t

> com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaVa

> lidator.startElement(XMLSchemaValidator.java:685)

> at

> t

> com.sun.org.apache.xerces.internal.jaxp.validation.DOM

> ValidatorHelper.beginNode(DOMValidatorHelper.java:273)

>

> at

> t

> com.sun.org.apache.xerces.internal.jaxp.validation.DOM

> ValidatorHelper.validate(DOMValidatorHelper.java:240)

> at

> t

> com.sun.org.apache.xerces.internal.jaxp.validation.DOM

> ValidatorHelper.validate(DOMValidatorHelper.java:186)

> at

> t

> com.sun.org.apache.xerces.internal.jaxp.validation.Val

> idatorImpl.validate(ValidatorImpl.java:100)

> at

> t

> javax.xml.validation.Validator.validate(Validator.java

> :127)

> at Test.main(Test.java:33)

>

> Is anyone aware of any reason this would happen?

> Thank you in advanc

Perhaps the problem lies somewhere within this class?

import org.w3c.dom.Document;

When i run into these kinds of problems I first try to find out if any third party JAR's or classes that I import will run correctly on the later version of the JRE.

Maybe they have a place on their site you can download the same class files developed for version 6?

maple_shafta at 2007-7-12 19:52:46 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi,Thanks for your response, much appreciated. The org.w3c.dom package and org.xml.sax package come as part of the java 6 library
megasteoa at 2007-7-12 19:52:46 > top of Java-index,Java Essentials,Java Programming...
# 3
Does the "name" element have to be in the "tst" namespace?Your xml file says:"tst:name" but "first" and "second" without the leading "tst:".Remove the "tst:", see if it works, then figure out how to set up the namespace correctly if you need it. :-)-Puce
Pucea at 2007-7-12 19:52:46 > top of Java-index,Java Essentials,Java Programming...
# 4

Normally to link a xml file to a schema file I would use something like this:

<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="myXsdFile">

But there might be other ways. Especially if you WANT to have the namespaces.

-Puce

Pucea at 2007-7-12 19:52:46 > top of Java-index,Java Essentials,Java Programming...
# 5
Set namespace aware to true.DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();dbf.setNamespaceAware(true);and check out this link: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6529766
Danniel_Williana at 2007-7-12 19:52:46 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks Danniel, that was it
megasteoa at 2007-7-12 19:52:46 > top of Java-index,Java Essentials,Java Programming...