4

环境:带有 EclipseLink MOXy 的 JAXB 2.1.2

要求:

我想在编组时得到这样的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<root id="id123">
   <email>test@gmail.com</email>
   <address type="short">...</address>
</root>

我用这两个类对此进行建模:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="root")
public class ClassA {

    @XmlAttribute(name="id")
    private String id = null;

    @XmlElement(name="address")
    private Address addr = new Address();

    // and some getters setters
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Address {

    @XmlElement(name="address")
    private String address = null;

    @XmlAttribute(name="type")
    private String type = null;

}

我得到的是这个,地址被嵌套两次:

<?xml version="1.0" encoding="UTF-8"?>
<root id="id123">
   <email>test@gmail.com</email>
   <address type="short">
      <address>...</address>
   </address>
</root>

如何删除一个层次结构?

4

1 回答 1

7

您可以执行以下操作@XmlValue

@XmlAccessorType(XmlAccessType.FIELD)
public class Address {

    @XmlValue
    private String address = null;

    @XmlAttribute(name="type")
    private String type = null;

}

了解更多信息

于 2013-01-09T15:01:48.700 回答