Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug JAXB unmarshalling?

I'm running into a problem with JAXB unmarshalling. I think I have it properly coded, but my unmarshalled object returns with null parameters. Consequently, I am assuming that when unmarshalling, JAXB is not seeing the appropriate XML structure that it is expecting. However, I do not get any error messages or any exceptions thrown.

Is there anyway to step through the unmarshalling process to see exactly where/why it is failing to populate my object(s)?

The actual unmarshalling code is fairly mundane:

public <T> T unmarshall(Node node, Class<T> clazz) throws JAXBException {
    // Creating an unmarshaller
    Unmarshaller u = JAXBContext.newInstance(clazz).createUnmarshaller();

    // unmarshal an instance node into  Java content
    return clazz.cast(u.unmarshal(node, clazz).getValue());
}

However, when I call it, I get an object of type clazz returned (as expected), but unpopulated.

The DOM object that I am trying to unmarshal is generated by a third party API. I have already run into some extremely odd behaviours with the unmarshalling, which is why I would like to be able to debug the process. For instance, if I try to unmarshal a sub-element within the DOM object (ie: doc.getByElementName("myElement").item(0)), it fails silently. However, if I convert the document to a string, and reimport it into a new document, then it converts it fine.

I am starting to get quite frustrated not knowing how to debug this issue.

Thanks for any insights!

Eric

like image 885
Eric B. Avatar asked Jan 05 '12 22:01

Eric B.


People also ask

What is JAXB unmarshalling?

The JAXB Unmarshaller interface is responsible for governing the process of deserializing the XML data to Java Objects. The unmarshalling to objects can be done to variety of input sources.

How do you Unmarshal string JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.

What is unmarshalling in Java?

The Unmarshaller class governs the process of deserializing XML data into newly created Java content trees, optionally validating the XML data as it is unmarshalled. It provides an overloading of unmarshal methods for many different input kinds.

What is ObjectFactory in JAXB?

jaxb package. An ObjectFactory allows you to programatically construct new instances of the Java representation for XML content. The Java representation of XML content can consist of schema derived interfaces and classes representing the binding of schema type definitions, element declarations and model groups.


2 Answers

JAXBContext context = JAXBContext.newInstance(jaxbObjectClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
like image 128
Oversteer Avatar answered Sep 18 '22 14:09

Oversteer


One approach you could take is to use JAXB to generate an XML schema from your annotated classes. This represents what JAXB expects the input document to look like. Then validate your XML document against this XML schema to see if it conforms to JAXB's expectations.

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema
like image 31
bdoughan Avatar answered Sep 20 '22 14:09

bdoughan