This discussion is archived
1 Reply Latest reply: Oct 10, 2012 8:12 AM by jtahlborn RSS

JAXB 1.0 RI ValidationEventHandler "Unexpected element" ERROR

672287 Newbie
Currently Being Moderated
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.

public class TestHandler implements ValidationEventHandler

     @Override
     public boolean handleEvent(ValidationEvent arg0)
     {
          System.out.println(arg0);
          return true;
     }

}

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

public static void main(String[] args) throws Exception
     {
          File f = new File("test.xml");
          JAXBContext jaxbC = JAXBContext.newInstance("generated");
          Unmarshaller unmarshaller = jaxbC.createUnmarshaller();
          unmarshaller.setEventHandler(new TestHandler());
          Person p = (Person) unmarshaller.unmarshal(f);
     }

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?
  • 1. Re: JAXB 1.0 RI ValidationEventHandler "Unexpected element" ERROR
    jtahlborn Expert
    Currently Being Moderated
    user10648625 wrote:
    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?
    you are asking about known issues in a tech which is ~7 years old at this point and long since no longer maintained? it sounds like you are starting a new project. why would you possibly be considering using jaxb 1.0?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points