如何使用JAXB对元素进行逻辑分组?

时间:2013-09-25 15:10:04

标签: java jaxb

我有Booking.java

@XmlRootElement(name="booking")
@XmlAccessorType(XmlAccessType.FIELD)
class Booking{
    @XmlElement(name="name", required = true, nillable = true)
    private String name;

    @XmlElement(name="contact",required = true, nillable = true)
    private int contact;

    @XmlElement(name="startDate", required = true, nillable = true)
    private Date startDate;

    @XmlElement(name="endDate", required = true, nillable = true)
    private Date endDate;

    @XmlElement(name="address", required = true, nillable = true)
    private String address;
}

我使用JAXB进行编组,输出显示如下

<booking>
    <name>RAHUL</name>
    <contact>9898989898</contact>
    <startDate>2013-08-12T00:00:00+05:30</startDate>
    <endDate>2013-01-12T00:00:00+05:30</endDate>
    <address>Mumbai</address>
</booking>

但我希望输出如下所示

<booking>
    <PersonalDetails>
          <name>RAHUL</name>
          <contact>9898989898</contact>
    </PersonalDetails>

    <startDate>2013-08-12T00:00:00+05:30</startDate>
    <endDate>2013-01-12T00:00:00+05:30</endDate>
    <address>Mumbai</address>
</booking>

我已经检查了XMLElementWrapper,但它适用于Collection。 注意:我无法更改我的pojo Booking.java

2 个答案:

答案 0 :(得分:0)

创建类PersonalDetails(带名称和联系人)并将其用作属性。

答案 1 :(得分:0)

如果您使用MOXy作为JAXB提供程序(我是MOXy),那么您可以在此用例中使用@XmlPath扩展名:

@XmlPath("PersonalDetails/name/text()")