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>
tagで指定した部分内の<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
ですか?
前もって感謝します