users@jaxb.java.net

Problem: Unmarshaller throws exception even if ValidationEventHandler returns true

From: Yakov Polonsky <ypolonsky_at_MARKETSWITCH.COM>
Date: Wed, 14 May 2003 11:25:52 -0600

It looks like ValidationEventHandler is not working the way
it suppose to work. If ValidationEvent has severity ERROR,
SAXUnmarshallerHandlerImpl throws UnmarshallException no matter what
handleEvent() method returns:

(This is just a sample to demonstrate)

Schema TEXTA.xsd :
-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >

  <xs:element name="TESTA" type="TESTA_type" />

  <xs:complexType name="TESTA_type">
    <xs:sequence>
      <xs:element name="x" type="xs:int" />
      <xs:element name="y" type="xs:int" />
    </xs:sequence>
  </xs:complexType>

</xs:schema>
-------------------------

I generated java files into the "generated" package.

Test source MarshallTest.java :
-------------------------
import generated.TESTA;

import javax.xml.bind.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class MarshallTest
{
   public static void main(String[] args)
   {
      try
      {
         JAXBContext jaxbContext =JAXBContext.newInstance("generated");

         Unmarshaller u = jaxbContext.createUnmarshaller();
         u.setEventHandler(new ValidationEventHandler()
         {
            public boolean handleEvent(ValidationEvent event)
            {
               System.err.println("Error: [" + event.getSeverity() + "] " + event.getMessage());
               // !!! HERE TRY TO CHANGE TO RETURN true !!!
               return false;
            }
         });
         TESTA beanA = (TESTA)u.unmarshal( new FileInputStream( "testa.xml" ) );
      }
      catch (javax.xml.bind.JAXBException e) { e.printStackTrace(); }
      catch (FileNotFoundException e) { e.printStackTrace(); }
   }
}
-------------------------

The incorrect xml (<z> node is not in schema):
-------------------------
<TESTA>
       <x>1</x>
       <z>5</z>
       <y>2</y>
</TESTA>
-------------------------

Exception after compiling and running:
-------------------------
Error: [1] Unexpected element {}:z
javax.xml.bind.UnmarshalException: Unexpected element {}:z
 at com.sun.xml.bind.unmarshaller.UnreportedException.createUnmarshalException(UnreportedException.java:58)
 at com.sun.xml.bind.unmarshaller.SAXUnmarshallerHandlerImpl.reportAndThrow(SAXUnmarshallerHandlerImpl.java:498)
 at com.sun.xml.bind.unmarshaller.SAXUnmarshallerHandlerImpl.startElement(SAXUnmarshallerHandlerImpl.java:112)
 at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1488)
 at org.apache.crimson.parser.Parser2.content(Parser2.java:1779)
   ...
-------------------------
Try to change "return false;" to "return true;" in the handleEvent() method. The
result exception will be the same.

My questions are:

1. I expect unmarshaller to continue the current unmarshal operation as it says
in API reference. Is it a bug or expected behavior?

2. I need to skip "unknown" tags in the XML even if the schema does not specify
them. This can be useful if the server update changed the data structure so that
additional tags appear. If client has older version it still expect recognize all
known tags according to the schema the client has been compiled with.
Any idea how to solve this problem?

Thank you,
Yakov Polonsky
ypolonsky_at_marketswitch.com