开发者

JAXB, how to skip class tag when included

开发者 https://www.devze.com 2023-03-05 05:36 出处:网络
I\'m trying to get that XML: <person> <foo>thing</foo> <bar>other</bar> </person>

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
0

精彩评论

暂无评论...
验证码 换一张
取 消