Search code examples
debuggingjaxbunmarshalling

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


Solution

  • 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.