2016-03-03 71 views
0

你会如何设计解决此XML的JAX-RS转换复杂的XML到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> 

回答

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() {} 

和具有详细类如下

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


    @XmlAttribute 
    private String key; 

    @XmlValue 
    private String value; 
+0

的关键点是有一些像列表类型的容器的细节附加到活动对象.. – integratedsolns

0

从XML文件JAXB元素,您可以定义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>