在從我的喜架構root.class的元素項和ohter對象是一個itemList中的一部分:JAXB可映射的XML元素
@XmlElementRef(name = "item", namespace = "xi", type = JAXBElement.class, required = false)
//...
protected List<Object> itemList;
我在ObjectFactory.class
已經從主架構的一些項目作爲JAXBElements這樣的:
@XmlElementDecl(namespace = "de-schema", name = "detailedInformation", substitutionHeadNamespace = "xi", substitutionHeadName = "item")
public JAXBElement<numItemType> createDetailedInformation(numItemType num) {
return new JAXBElement<numItemType>(_detailedInformation_QNAME, numItemType.class, null, num);
}
所以numItemType先後爲JAXBElement
一些屬性和價值(NUM)。
NumItemType.class:
@XmlJavaTypeAdapter(numItemTypeAdapter.class)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "numItemType", namespace = "xi", propOrder = {
"num"
})
public class NumItemType {
@XmlValue
protected BigDecimal num;
@XmlAttribute(name = "precision")
protected String precision;
@XmlAttribute(name = "decimals")
protected String decimals;
//... more Attributes
}
但當JAXB
解組的XML
文件,它會只有要素,例如:
<detailedInformation>
<element1>1234</element1>
<element2>5678</element2>
<element3>bla</element3>
</detailedInformation>
當我名帥,它應該成爲(像JAXB java代碼):
<detailedInformation element2="5678" element3="bla">1234</detailedInformation>
因此,我寫了numItemTypeAdapter.class與 NumItemTypeAdapter延伸XmlAdapter
AdaptedNum.class:
public class AdaptedNum {
@XmlElement
private double element1;
@XmlElement
private String element2;
@XmlElement
private String element3;
/** Some getter/setter methods */
}
我想,這將是幫助我http://blog.bdoughan.com/2012/02/xmlanyelement-and-xmladapter.html,但它畢竟是一個有點棘手: -/