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]

> 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?