将Complex xml转换为JAXB

时间:2016-03-03 19:13:38

标签: jaxb jax-rs

如何围绕此xml为JAX-RS设计一个jaxb元素

<activity>
    <code>Purchase</code>
    <description> Purchase and sell </description>
    <deals>
        <deal key="name"> buy</deal>
        <deal key="isactive"> True </deal>
        <deal key="isgood"> False </deal>
        <deal key="costcode"> FINCOM </deal>
        <deal key="opportunity"> Finance</deal>
    <deals>
</activity>

2 个答案:

答案 0 :(得分:0)

从XML文件中,您可以定义XML模式(请参阅下面的示例),然后从中生成Java绑定。

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="activity">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="code" type="xsd:string" />
            <xsd:element name="description" type="xsd:string" />
            <xsd:element name="deals">
                <xsd:complexType>
                    <xsd:sequence maxOccurs="unbounded">
                        <xsd:element name="deal">
                            <xsd:complexType>
                                <xsd:simpleContent>
                                    <xsd:extension base="xsd:string">
                                        <xsd:attribute name="key" type="xsd:string" />
                                    </xsd:extension>
                                </xsd:simpleContent>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

答案 1 :(得分:0)

   @XmlRootElement(name = "activity")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class Activity {

    @XmlElement
    private String code;


   @XmlElement
   private String description ;


   @XmlElementWrapper
   @XmlElement(name="detail")
   public List<Detail> details = new ArrayList<Detail>();

   public Activity() {}

并有一个Detail类,如下所示

   @XmlRootElement(name = "detail")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class Detail {


   @XmlAttribute
   private String key;

   @XmlValue
   private String value;