我正在尝试获取XML:
<person>
<foo>thing</foo>
<bar>other</bar>
</person>
从那个代码
public class Person {
private InnerThing in;
}
public class InnerThing {
private String foo;
private String bar;
}
与JAXB Java注释。
默认情况下,我得到
<person>
<innerThing>
<foo>thing</foo>
<bar>other</bar>
</innerThing>
</person>
如何跳过仅带有注释的InnerThing标签?
最佳答案
您可以使用EclipseLink JAXB (MOXy)的@XmlPath注释解决此问题(我是MOXy技术负责人):
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlPath(".")
private InnerThing in;
}
想要查询更多的信息:
http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html