JAXB, how to skip class tag when included
I'm trying to get that XML:
<person>
<foo>thing</foo>
<bar>other</bar>
</person>
from that code
public class Person {
private InnerThing in;
}
public class InnerThing {
private String foo;
private String bar;
}
with JAXB java annotations.
By default, I get
<person>
<innerThing>
<foo>thing</foo>
<bar>other</bar>开发者_Go百科
</innerThing>
</person>
How can I skip the InnerThing tag with just annotations?
You could use EclipseLink JAXB (MOXy)'s @XmlPath annotation to solve this problem (I'm the MOXy tech lead):
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;
}
For More Information:
- 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
精彩评论