Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Unmarshall Created an Empty Object

I am using JAXB to unmarshal an XML file into an object tree. The root object is non-null, but all of it's members are null even though there is data in the XML file. My object classes were generated with Axis2 from wsdls. I have the ObjectFactory class, the jaxb.index class, the package.info annotation, etc.

My problem is the same as this discussion: http://old.nabble.com/AXIS2,-JAXB---Unmarshalling-td26847419.html

First and foremost: is there a way I can get it to fill the data properly?

If not, is there a good substitute library for either Axis2 or JAXB that does the same thing but that will play nicely with each other?

like image 906
Amber Shah Avatar asked Apr 13 '10 21:04

Amber Shah


2 Answers

JAXB by default silently ignores errors. I cannot see any reason why this is the default configuration. Try adding this code to throw an exception if something goes wrong.

unmarshaller.setEventHandler(
    new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent event ) {
            throw new RuntimeException(event.getMessage(),
                                       event.getLinkedException());
        }
});
like image 137
Jesse Barnum Avatar answered Oct 05 '22 23:10

Jesse Barnum


Recently I had similar kind of issue, and could able to fix it up as below:

1) Fixup the xsd file. In my case I've ensured that all complex elements in xsd refering to their corresponding type using ref attribute, instead of declaring them with type attribute.

I've verified whether my xsd proper or not by creating an xml file from XML Schema file option in Eclipse.

Before the fix, the root element in the xml file was empty. After fixing the xsd the xml was got created properly with some sample values.

2) clean and re-build It's mandatory to clean before re-generating the classes.

like image 32
Srinivas Avatar answered Oct 06 '22 00:10

Srinivas