7

我正在尝试使用 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类?

提前致谢

4

1 回答 1

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());
}
于 2012-10-05T17:55:25.830 回答