Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB unmarshalling with java generics

I am trying to unmarshall an XML document from a legacy system using JAXB. I have an xml structure as follows :

<response>
    <id>000000</id>
    <results>
        <result>
<!-- Request specific xml content -->
            <year>2003</year>
            <title>Lorem Ipsum</title>
            <items>
                <item>I1</item>
                <item>I2</item>
            </items>
        </result>
        <result>
            <year>2007</year>
            <title>Dolor sit amet</title>
            <items>
                <item>K1</item>
                <item>K2</item>
            </items>
        </result>
    </results>
</response>

The tags inside the part specified by <result> tag will change depending on my request. Since the content may change I decided to use generics for result items and I have prepared my java beans with annotations as following:

// imports here
@XmlRootElement(name="response")
@XmlAccessorType(XmlAccessType.FIELD)
public class XResponse<T>{
    private String id;

    @XmlElementWrapper(name="results")
    @XmlElement(name="result")
    private List<T> results;

// setters and getters
}

...

@XmlRootElement(name="result")
@XmlAccessorType(XmlAccessType.FIELD)
public class X1Result{
    private String year;
    private String title;
    @XmlElementWrapper(name="items")
    @XmlElement(name="item")
    private List<String> items;

// setters and getters
}
...

I tried unmarshalling the xml document via the code below:

JAXBContext context = JAXBContext.newInstance(XResponse.class, X1Result.class);
Unmarshaller um = context.createUnmarshaller();
XResponse<X1Result> response = (XResponse<X1Result>) um.unmarshal( xmlContent );

List<X1Result> results = unmarshal.getResults();
for (X1Result object : results) {
    System.out.println(object.getClass());
}

I have a problem during the unmarshalling that it can't cast the list items into X1Result class. Instead it uses org.apache.xerces.dom.ElementNSImpl.

What should I do to make JAXB Unmarshaller use X1Result class?

Thanks in advance

like image 885
Alper Avatar asked Oct 05 '12 17:10

Alper


People also ask

How do you Unmarshal XML string to Java object using 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.

Is JAXB Marshaller thread safe?

The rules for JAXB in a multi-threaded environment are very simple: you can share the JAXBContext object among threads. Doing so will also improve performance, as the construction of the context may be expensive. All other objects, including Marshaller and Unmarshaller, are not thread-safe and must not be shared.

What is Java Marshaller?

The Marshaller class is responsible for governing the process of serializing Java content trees back into XML data.


1 Answers

I think you should use inheritance instead of generics. Given an XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <id>000000</id>
    <results>
        <result xsi:type="X1Result">
            <year>2003</year>
            <title>Lorem Ipsum</title>
            <items>
                <item>I1</item>
                <item>I2</item>
            </items>
        </result>
        <result xsi:type="X1Result">
            <year>2007</year>
            <title>Dolor sit amet</title>
            <items>
                <item>K1</item>
                <item>K2</item>
            </items>
        </result>
    </results>
</response>

You can dynamically bind your <result> entries. You have a top-level type:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "XResult")
@XmlSeeAlso({
    X1Result.class
})public abstract class XResult {

}

And you have implementing classes:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "X1Result")
public class X1Result extends XResult {
    @XmlElement(name = "year")
    private String year;

    @XmlElement(name = "title")
    private String title;

    @XmlElementWrapper(name = "items")
    @XmlElement(name = "item")
    private List<String> items;
    ...
}

Use the top-level type in your XResponse class:

@XmlRootElement(name = "response")
@XmlAccessorType(XmlAccessType.FIELD)
public class XResponse {
    @XmlElement(name = "id")
    private String id;

    @XmlElementWrapper(name = "results")
    @XmlElement(name = "result")
    private List<XResult> results;
    ...
}

And you can unmarshall using the top-level type:

context = JAXBContext.newInstance(XResponse.class, XResult.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
XResponse response = (XResponse) unmarshaller.unmarshal(new File("testfile.xml"));

List<XResult> results = response.getResults();
for (XResult object : results) {
    System.out.println(object.getClass());
}
like image 109
davidfmatheson Avatar answered Oct 02 '22 02:10

davidfmatheson