jaxb marshal包括没有root标签的类

时间:2016-11-11 09:08:11

标签: java jaxb

我需要包含类字段的marshal类,但不包含类根标记。例如,我有类Person和Address:

@XmlRootElement(name="address")
class Address {
    ...

    @XmlElement
    String street;
}

@XmlRootElement(name="person")
class Person {
    ...

    @XmlElement
    Address address;
}

如果我整理它,我会得到:

<person>
  ...
  <address>
    <street>...<street>
  <address>
</person>

但我需要:

<person>
  ...
  <street>...<street>
</person>

1 个答案:

答案 0 :(得分:0)

可能的解决方案可能是..

@XmlRootElement(name="address")
class Address {
    ...

    @XmlElement
    String street;
}

@XmlRootElement(name="person")
class Person {
    ...

    @XmlTransient
    Address address;

    @XmlElement
    public String getStreet() {
       return (address!= null) ? address.getStreet() : null;
    }
}