JDK 1.6 and XML validation failure

Hello, everyone!

Well, I think there are some people who tried to test their code on JDK 1.6 beta...

I've discovered some problems but I don't know what's the reason of such strange effect.

Here is my small XML-validator. I've compiled one of the examples onsun.com.

import org.xml.sax.SAXException;

import org.w3c.dom.Document;

import javax.xml.validation.*;

import javax.xml.*;

import javax.xml.transform.dom.DOMSource;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import java.io.*;

publicclass Main

{

publicstaticvoid main(String[] args)

{

try

{

if (args.length!=2)

{

System.out.println("Usage: [XML-file] [XSD-file]");

return;

}

long t1 = System.currentTimeMillis();

long t3 = System.currentTimeMillis();

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

Schema schema = factory.newSchema(new File(args[1]));

Validator validator = schema.newValidator();

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

long t2 = System.currentTimeMillis();

System.out.println("Init: "+(t2-2*t3+t1));

t1 = System.currentTimeMillis();

t3 = System.currentTimeMillis();

Document document = parser.parse(new File(args[0]));

validator.validate(new DOMSource(document));

t2 = System.currentTimeMillis();

System.out.println("Done: "+(t2-2*t3+t1));

}catch(SAXException e){

e.printStackTrace();

}catch(ParserConfigurationException e){

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}

}

}

Here is my XML file:

<birthdate>

<month>January</month>

<day>21</day>

<year>1983</year>

</birthdate>

Here is my schema:

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

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

<xsd:element name="birthdate">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="month" type="xsd:string" />

<xsd:element name="day" type="xsd:int" />

<xsd:element name="year" type="xsd:int" />

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

The validation succeed on JDK 1.5.0_08 and fails with an exception on JDK 1.6.0 beta 2:

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

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 Main.main(Main.java:45)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at ...

Could anyone explain why?

[5769 byte] By [Covert.Deva] at [2007-10-3 4:09:05]
# 1

Cannot find the declaration of element 'birthdate'.

Specify the schema location in the XML document with

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

xsi:noNamespaceSchemaLocation=

"file://c:/Schemas/schema.xsd"

dvohra09a at 2007-7-14 22:09:01 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Well, may be this is ok. But what about specifying full path to XSD? I'm liading it from JAR and I don't even know it's name. (It's in my classpath). Is this specification strongly needed?
Covert.Deva at 2007-7-14 22:09:01 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
schema location is required to be specified.For JAR file URL refer http://javaalmanac.com/egs/java.net/JarUrl.html
dvohra09a at 2007-7-14 22:09:01 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

Well. But what if the schema is only a byte array? I've loaded this array from somewhere and I need to validate some incoming XML-messages. And if the schema URL is strongly required how to do this?

I don't want my app to connect somewhere and it's not very easy to understand full path to the JAR. It can be located somewhere in tomcat/webapps.

Covert.Deva at 2007-7-14 22:09:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

I have run into this same bug. I have also tried Xerces-J 2.8.1 and get the same result. When I use the library to do validation while reading the DOM object, the example birthdate schema and xml validate, but If I try to use the validation package as documented here (http://java.sun.com/developer/technicalArticles/xml/validationxpath/) it fails.

Here is the code I was able to get to work:

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main

{

public static void main(String[] args)

{

try

{

if (args.length!=2)

{

System.out.println("Usage: [XML-file] [XSD-file]");

return;

}

long t1 = System.currentTimeMillis();

long t3 = System.currentTimeMillis();

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

//Schema schema = factory.newSchema(new File(args[1]));

//Validator validator = schema.newValidator();

FileInputStream is = new FileInputStream(new File(args[1]));

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");

dbf.setAttribute( "http://java.sun.com/xml/jaxp/properties/schemaSource", is);

dbf.setValidating(true);

DocumentBuilder parser = dbf.newDocumentBuilder();

long t2 = System.currentTimeMillis();

System.out.println("Init: "+(t2-2*t3+t1));

t1 = System.currentTimeMillis();

t3 = System.currentTimeMillis();

Document document = parser.parse(new File(args[0]));

//validator.validate(new DOMSource(document));

t2 = System.currentTimeMillis();

System.out.println("Done: "+(t2-2*t3+t1));

}catch(SAXException e){

e.printStackTrace();

}catch(ParserConfigurationException e){

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}

}

}

You can set the schemaSource to be a path, file or stream. It would be nice however if the validation package still worked as it did in all of the 1.5 releases.

ryusaeba42a at 2007-7-14 22:09:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...