aspose file tools*
The moose likes XML and Related Technologies and the fly likes JAXB 1.0 RI ValidationEventHandler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JAXB 1.0 RI ValidationEventHandler "Unexpected element" ERROR" Watch "JAXB 1.0 RI ValidationEventHandler "Unexpected element" ERROR" New topic
Author

JAXB 1.0 RI ValidationEventHandler "Unexpected element" ERROR

Pavan Upadhya
Greenhorn

Joined: Dec 12, 2008
Posts: 8
Hi,

I am using JAXB1.0 RI libraries and I am seeing a strange issue. Here is what I see

I have this simple XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="0"/>
<xs:element name="Age" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


I used xjc compiler (which is compliant with JAXB 1.0 version) to generate Java code and it generated bunch of java files. Here is the content of MANIFEST.MF file inside xjc jar file

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.2
Created-By: 1.4.1_02-b06 (Sun Microsystems Inc.)
Specification-Title: Java Architecture for XML Binding
Specification-Version: 1.0
Specification-Vendor: Sun Microsystems, Inc.
Implementation-Title: JAXB Reference Implementation
Implementation-Version: 1.0.4
Implementation-Vendor: Sun Microsystems, Inc.
Implementation-Vendor-Id: com.sun


In my client code I have a class which implements ValidationEventHandler to ignore unknown elements.



Everything works fine as long as I try to unmarshall an xml which has valid tags according to the xsd. But when I try to unmarshall an xml which has an unknown tag (shown below), I expected the handleEvent method in my validation handler to get called only for that particular tag which is unknown. But I am seeing handle Event being called for all the tags which follows the unknown tag.

Here is the XML input which contains one invalid tag (Junk)

<Person>
<Junk>123</Junk>
<Name>A</Name>
<Age>12</Age>
</Person>


Here is the sample client code



Here is the output

[severity=ERROR,message=Unexpected element {}:Junk,locator=[node=null,object=null,url=file:/c:/test.xml,line=2,col=8,offset=-1]]
[severity=ERROR,message=Unexpected element {}:Name,locator=[node=null,object=null,url=file:/c:/test.xml,line=3,col=8,offset=-1]]
[severity=ERROR,message=Unexpected element {}:Age,locator=[node=null,object=null,url=file:/c:/test.xml,line=4,col=7,offset=-1]]


I was expecting Unexpected element only for <Junk> tag, but I got the error for other 2 tags as well.

One interesting observation is, if I modify the XSD to remove minOccurs Indicator as shown below

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name"/>
<xs:element name="Age"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

and I regenerate the java files and pass in the same XML which contains invalid tag <Junk> and when I unmarshall the same xml, I see the handleEvent method in my handler is being called only for the unknown tag <Junk>

[severity=ERROR,message=Unexpected element {}:Junk,locator=[node=null,object=null,url=file:/c:/test.xml,line=2,col=8,offset=-1]]

When I use xjc JAXB2.0 compliant compiler, I do not see this behavior. Irrespective of the minOccurs Indicator being present or not, unmarshalling works and handleEvent method gets called only for the unknown tag.

Is this a known issue in JAXB1.0 RI or am I missing something here?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXB 1.0 RI ValidationEventHandler "Unexpected element" ERROR