tags:

views:

1531

answers:

2

How can one ignore Unexpected element situation in JAXB ans still get all other kind of javax.xml.bind.UnmarshalException?

obj = unmler.unmarshal(new StringReader(xml))

Notice i still want to get the obj result of the xml parsing.

+2  A: 

The solution.

In JAXB implementing ValidationEventHandler like so:

class CustomEventHandler implements ValidationEventHandler{

 public boolean handleEvent(ValidationEvent evt) {
  System.out.println("Event Info: "+evt);
  if(evt.getMessage().contains("Unexpected element"))
   return true;
  return false;
 }

}

Then

Unmarshaller u = ...;

u.setEventHandler(new CustomValidationEventHandler() );

u.unmarshal(new StringReader(xml));

João
A: 

I think that should be the other way around, shouldn't it? If the event pertains to an unexpected element, you want to return true so that things continue.