4
我有一个非常简单的类,有两个字段,String sourceAddress和int port。
我希望它们被映射到源/地址和源/端口节点上,而不是 jaxb的默认sourceAddress和sourcePort。
所以我使用MOXy @XmlPath注释。
的问题是,将注释不理,我也得到了“JAXB默认”的XML文件:MOXy @XmlPath忽略
<szk>
<sourceAddress>test</sourceAddress>
<sourcePort>10000</sourcePort>
</sz>
在此先感谢您的帮助 阿戈斯蒂诺
import javax.xml.bind.*; import javax.xml.bind.annotation.*; import org.eclipse.persistence.jaxb.JAXBContext; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class SZK { @XmlPath("source/address") private String sourceAddress; @XmlPath("source/port") private int sourcePort; public static void main (String [] args) throws JAXBException{ SZK k = new SZK(); k.sourceAddress = "test"; k.sourcePort = 10000; javax.xml.bind.JAXBContext jc = JAXBContext.newInstance(SZK.class); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(k, System.out); } }
直接从“源头”回答:-)。是的,这是缺少jaxb.properties文件。我对这些文件进行了一些更多的测试,以确认每个软件包可以有不同的提供者。好的,直到我试图从JAXBContext.newInstance(pkg1.C1.class,pkg2.C2.class)封装不同包中的类...但这是一个相当先进的问题。 – AgostinoX 2011-05-10 22:46:58
如果您正在使用maven,请在属性文件的文件系统上检查您的目标/类....也是如此。以防万一maven被设置为不将文件复制到目标中 – Derek 2013-11-28 11:33:00