6
我想從使用JAXB的遺留系統解開XML文檔。我有一個XML結構如下:使用java泛型的JAXB解組
<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與註解如下:
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());
}
:
// 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文檔我在解組期間遇到問題,無法將列表項目轉換爲X1Result
類。相反,它使用org.apache.xerces.dom.ElementNSImpl
。
我該怎麼做才能讓JAXB Unmarshaller使用X1Result
類?
在此先感謝
上面的代碼init ializes'XResult'而不是'X1Result',因此它是錯誤的,因爲它是一個抽象類。除了xml的內容來自遺留系統,並且沒有可用的'xsi:type'信息。 (儘管我可以通過編程方式添加它) – Alper
如果'xsi:type'存在,代碼將實例化一個'X1Result'。至少,這就是它爲我做的。祝你好運。 – davidfmatheson
這對我來說沒有XML中存在'xsi:type'的問題。我正在使用Java 1.7.0_15。 –