【发布时间】:2011-08-22 09:05:20
【问题描述】:
我有一个非常简单的类,它有两个字段,String sourceAddress 和 int port。
我希望它们映射到源/地址和源/端口节点而不是
jaxb 默认的 sourceAddress 和 sourcePort。
所以我使用 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); } }【问题讨论】:
-
您好,感谢您提出这个问题。实际上,我也在尝试做类似的事情,但由于某种原因,它没有按预期工作。我也尝试了这个答案中提到的东西。我尝试了这种方法和作者博客中的示例,但
JAXB Marshalling仍然无法按预期使用@XmlPath。似乎使用和不使用@XmlPath生成的 XML 是相同的。请您查看此示例并提供您的答案:stackoverflow.com/questions/67500565/…
标签: jaxb eclipselink moxy