JAXB使用java泛型进行解组

时间:2012-10-05 17:10:32

标签: java jaxb unmarshalling

我正在尝试使用JAXB从遗留系统解组XML文档。我有一个xml结构如下:

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

<result>标记指定的部分内的标记会根据我的要求而变化。由于内容可能会发生变化,我决定对结果项使用泛型,我已经准备好了带有注释的java bean:

// 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
}
...

我尝试通过以下代码解组xml文档:

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());
}

我在解组时遇到问题,它无法将列表项强制转换为X1Result类。相反,它使用org.apache.xerces.dom.ElementNSImpl

如何使JAXB Unmarshaller使用X1Result类?

提前致谢

1 个答案:

答案 0 :(得分:2)

我认为你应该使用继承而不是泛型。给出这样的XML:

<?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>

您可以动态绑定<result>条目。您有一个顶级类型:

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

}

你有实施课程:

@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;
    ...
}

使用XResponse类中的顶级类型:

@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;
    ...
}

您可以使用顶级类型解组:

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());
}