在我的xi-schema的root.class中,元素item和其他对象是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具有
JAXBElement
的一些属性和value(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会有所帮助,但这有点棘手:-/
最佳答案
准确找出可能发生问题的位置有些棘手。我假设您的原始模型是从XML Schema生成的,这应该可以不做任何修改就可以正常工作。我在下面尝试提供示例的按比例缩小版本,这可能会有所帮助。
根
package forum11343610;
import java.util.*;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElementRef(name = "item", namespace = "xi", type = JAXBElement.class, required = false)
protected List<Object> itemList = new ArrayList<Object>();
public List<Object> getItemList() {
return itemList;
}
public void setItemList(List<Object> itemList) {
this.itemList = itemList;
}
}
NumItemType
package forum11343610;
import java.math.BigDecimal;
import javax.xml.bind.annotation.*;
@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
}
对象工厂
package forum11343610;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private static final QName _detailedInformation_QNAME = new QName("de-schema", "detailedInformation");
@XmlElementDecl(namespace = "xi", name = "item")
public JAXBElement<NumItemType> createItem(NumItemType num) {
return new JAXBElement<NumItemType>(_detailedInformation_QNAME, NumItemType.class, null, num);
}
@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);
}
}
演示版
package forum11343610;
import java.math.BigDecimal;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class, ObjectFactory.class);
ObjectFactory objectFactory = new ObjectFactory();
Root root = new Root();
NumItemType numItemType = new NumItemType();
numItemType.num = BigDecimal.TEN;
numItemType.decimals = "1";
numItemType.precision = "2";
root.getItemList().add(objectFactory.createDetailedInformation(numItemType));
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
输出量
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:ns2="de-schema" xmlns:ns3="xi">
<ns2:detailedInformation precision="2" decimals="1">10</ns2:detailedInformation>
</root>