我想知道是否有可能將xml元素解組到多個pojos。例如:JAXB unmarshall到多個pojo的
用於XML:
<type>
<id>1</id>
<cost>12</cost>
<height>15</height>
<width>13</width>
<depth>77</depth>
</type>
Item類
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name="type")
public class Item {
private Integer id;
private Double cost;
@XmlElement(name="id")
public Integer getId(){
return id;
}
@XmlElement(name="cost")
public Double getCost(){
return cost
}
}
ItemDimensions類
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name="type")
public class ItemDimensions {
private Integer height;
private Integer width;
private Integer depth;
@XmlElement(name="height")
public Integer getHeight(){
return height;
}
@XmlElement(name="width")
public Integer getWidth(){
return width;
}
@XmlElement(name="depth")
public Integer getDepth(){
return depth;
}
}
我曾嘗試使用許多由產生JAXB映射來完成類似的東西Netbeans 6.9和一些測試類,但現在已經得到了。有誰知道這是否可以在沒有任何中介對象的情況下完成?
AFAIK您將不得不自己創建ItemDimensions對象。例如返回新的ItemDimensions(高度,寬度,深度)。 – nabeelmukhtar 2011-04-25 15:13:45