在我的 xi-schema 的 root.class 中,元素 item 和 ohter 对象是 itemList 的一部分:
@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 有一些属性和 value(num) JAXBElement
。
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 extends XmlAdapter 编写了一个 numItemTypeAdapter.class
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,但这有点棘手:-/