我有一个非常简单的类,有两个字段,String sourceAddress 和 int port。
我希望它们映射到源/地址和源/端口节点,而不是 jaxb 默认的源地址和源端口。
所以我使用 MOXy @XmlPath 注释。
问题是注释被忽略了,我得到了“jaxb default”xml文件:
<szk>
<sourceAddress>test</sourceAddress>
<sourcePort>10000</sourcePort>
</sz>
提前感谢阿戈斯蒂诺的任何帮助
导入 javax.xml.bind.*;
导入 javax.xml.bind.annotation.*;
导入 org.eclipse.persistence.jaxb.JAXBContext;
导入 org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公共课 SZK {
@XmlPath("源/地址")
私有字符串源地址;
@XmlPath("源/端口")
私有 int 源端口;
公共静态 void main (String [] args) 抛出 JAXBException{
SZK k = 新 SZK();
k.sourceAddress = "测试";
k.sourcePort = 10000;
javax.xml.bind.JAXBContext jc = JAXBContext.newInstance(SZK.class);
编组器 m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(k, System.out);
}
}