problem while xml validation with jdk1.6, but at jdk1.5 code is running ok.

Hi All,

i have written code to read a xml file to validate it with schema

and it is running fine with jdk1.5 but when i run the same code using jdk1.6

it gives the following exception.

Please help me to resolve this problem.

Thanks in Advance

Sunny Kumar.

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

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 src.ShapeCodeParser.testValidator(ShapeCodeParser.java:462)

at src.ShapeCodeParser.main(ShapeCodeParser.java:533)

java code:

////////////////////////////////////////////////////////////////////////////////////////////////////////////

try{

//parse an XML document into a DOM tree

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

Document document = parser.parse(new File("shipOrder.xml"));

// create a SchemaFactory capable of understanding WXS schemas

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

// load a WXS schema, represented by a Schema instance

Source schemaFile = new StreamSource(new File("shipOrder.xsd"));

Schema schema = factory.newSchema(schemaFile);

// create a Validator instance, which can be used to validate an instance document

Validator validator = schema.newValidator();

// validate the DOM tree

validator.validate(new DOMSource(document));

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

}catch (SAXException e) {

// instance document is invalid!

e.printStackTrace();

//System.out.println("Validation UnSuccesful");

}catch(Exception e){

e.printStackTrace();

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

shipOrder.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923"

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

xsi:noNamespaceSchemaLocation="shiporder.xsd">

<orderperson>John Smith</orderperson>

<shipto>

<name>Ola Nordmann</name>

<address>Langgt 23</address>

<city>4000 Stavanger</city>

<country>Norway</country>

</shipto>

<item>

<title>Empire Burlesque</title>

<note>Special Edition</note>

<quantity>1</quantity>

<price>10.90</price>

</item>

<item>

<title>Hide your heart</title>

<quantity>1</quantity>

<price>9.90</price>

</item>

</shiporder>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

shipOrder.xsd

<?xml version="1.0" encoding="ISO-8859-1" ?>

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

<xs:element name="shiporder">

<xs:complexType>

<xs:sequence>

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

<xs:element name="shipto">

<xs:complexType>

<xs:sequence>

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

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

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

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

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name="item" maxOccurs="unbounded">

<xs:complexType>

<xs:sequence>

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

<xs:element name="note" type="xs:string" minOccurs="0"/>

<xs:element name="quantity" type="xs:positiveInteger"/>

<xs:element name="price" type="xs:decimal"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

<xs:attribute name="orderid" type="xs:string" use="required"/>

</xs:complexType>

</xs:element>

[5409 byte] By [sun@2005a] at [2007-11-27 7:58:18]
# 1

Replace this line:DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();

with these ones:DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

dbf.setNamespaceAware(true);

DocumentBuilder parser = dbf.newDocumentBuilder();

For more details see:

http://weblogs.java.net/blog/spericas/archive/2007/03/xml_schema_vali.html

I think Sun could easily contribute to lowering the number of the posts concerning this problem by updating the relevant code snippet in the following article:

http://java.sun.com/developer/technicalArticles/xml/validationxpath/

prgguya at 2007-7-12 19:40:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Thank you so much ....... Prgguy Now code is running fine. Thanks a lot.
sun@2005a at 2007-7-12 19:40:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...