2012-09-04 222 views
2

如何使用JAXB實現此XML?目前grantA,C,B元素中的expires屬性不受限制 - 但它應該是。我不知道如何將元素與屬性關聯。我是否必須爲每個授權A,C,B創建類?
XML:jaxb:將屬性綁定到元素

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <customers> 
     <customer> 
      <name>C1</name> 
      <grantA expires="">false</grantA> 
      <grantB expires="">true</grantB> 
      <grantC expires="">true</grantC> 
     </customer> 
     <customer> 
      <name>C2</name> 
      <grantA expires="">false</grantA> 
      <grantB expires="">true</grantB> 
      <grantC expires="">true</grantC> 
     </customer> 
     <customer> 
      <name>C3</name> 
      <grantA expires="">false</grantA> 
      <grantB expires="">true</grantB> 
      <grantC expires="">false</grantC> 
     </customer> 
    </customers> 

XSD:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="name" type="xs:string" /> 

    <xs:element name="grantA" type="xs:boolean" /> 
    <xs:element name="grantB" type="xs:boolean" /> 
    <xs:element name="grantC" type="xs:boolean" /> 

    <xs:element name="customers" type="customers" /> 
    <xs:element name="customer"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="name" /> 
       <xs:element name="grantA"> 
        <xs:complexType> 
         <xs:simpleContent> 
          <xs:extension base="xs:string"> 
           <xs:attribute name="expires" type="xs:string" /> 
          </xs:extension> 
         </xs:simpleContent> 
        </xs:complexType> 
       </xs:element> 
       <xs:element name="grantB"> 
        <xs:complexType> 
         <xs:simpleContent> 
          <xs:extension base="xs:string"> 
           <xs:attribute name="expires" type="xs:string" /> 
          </xs:extension> 
         </xs:simpleContent> 
        </xs:complexType> 
       </xs:element> 
       <xs:element name="grantC"> 
        <xs:complexType> 
         <xs:simpleContent> 
          <xs:extension base="xs:string"> 
           <xs:attribute name="expires" type="xs:string" /> 
          </xs:extension> 
         </xs:simpleContent> 
        </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    <xs:complexType name="customers"> 
     <xs:sequence> 
      <xs:element ref="customer" minOccurs="0" maxOccurs="unbounded" /> 
     </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

我現在的狀態是:

Customers.java:

@XmlRootElement(name = "customers") 
public class Customers 
{ 
    private List<Customer> customerList; 

    @XmlElement(name = "customer") 
    public List<Customer> getCustomerList() 
    { 
     if (null == customerList) 
     { 
      customerList = new ArrayList<Customer>(); 
     } 
     return customerList; 
    } 

    public void setCustomerList(List<Customer> customers) 
    { 
     this.customerList = customers; 
    } 

} 

Customer.java

@XmlRootElement(name = "customer") 
@XmlType(propOrder = { "name", "grantA", "grantB", "grantC" }) 
public class Customer 
{ 
    private String name = ""; 
    private boolean grantA; 
    private boolean grantB; 
    private boolean grantC; 

    @XmlJavaTypeAdapter(CollapsedStringAdapter.class) 
    @XmlElement(name = "name", required=true) 
    public String getName() 
    { 
     return name; 
    } 

    public void setName(String name) 
    { 
     this.name = name; 
    } 

    @XmlElement(name = "grantA", required=true) 
    public boolean isGrantA() 
    { 
     return grantA; 
    } 

    public void setGrantA(boolean grantA) 
    { 
     this.grantA = grantA; 
    } 



    @XmlElement(name = "grantB", required=true) 
    public boolean isGrantB() 
    { 
     return grantB; 
    } 

    public void setGrantB(boolean grantB) 
    { 
     this.grantB = grantB; 
    } 

    @XmlElement(name = "grantC", required=true) 
    public boolean isGrantC() 
    { 
     return grantC; 
    } 

    public void setGrantC(boolean grantC) 
    { 
     this.grantC = grantC; 
    } 


    @Override 
    public String toString() 
    { 
     return "" + name + "[ " + grantA + ", " + grantB + " " + grantC + " ]"; 
    } 

} 

回答

3

您可以創建一個類,Grant,像這樣:

@XmlType 
public class Grant { 
    @XmlAttribute 
    private String expires; 

    @XmlValue 
    private boolean value; 

    //getters and setters 
} 

,並在Customer類映射它以不同的名稱,例如:

public class Customer 
{ 
    private String name = ""; 
    @XmlElement(name="grantA"); 
    private Grant grantA; 

    @XmlElement(name="grantB"); 
    private Grant grantB; 

    @XmlElement(name="grantC"); 
    private Grant grantC; 

    //rest of the code 
} 
1

您可以使用一個類來表示具有expires屬性的布爾值。 XmlValue註釋用於表示其中一個屬性作爲元素的內容,而另一個屬性可註釋爲XmlAttribute

@XmlType 
class BooleanWithExpires { 
    @XmlValue public boolean value; 
    @XmlAttribute public String expires; 
} 

然後你可以使用這種類型的,而不是針對boolean不同批成員。無論您是想使用此類將數據存儲在您的Customer對象中,還是以其他方式存儲信息並僅在內部存儲器和BooleanWithExpires之間在XML子元素的獲取器和設置器中進行轉換。

如果更符合您的編碼風格,可以隨意將代碼從公共成員更改爲getter和setter。此外,請確保使用最適合expires的任何類型,例如XMLGregorianCalendar。如果這適合您,您可能還需要根據需要表示expires屬性。