1

我有 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

4

2 回答 2

0

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

于 2013-09-25T15:19:28.097 回答
0

如果您使用 MOXy 作为您的 JAXB 提供者(我是 MOXy)领导,您可以使用@XmlPath此用例的扩展:

@XmlPath("PersonalDetails/name/text()")
于 2013-09-25T17:06:38.993 回答