2012-09-24 29 views
6

Chúng tôi có một lược đồ XSD với một bản tuyên bố như thế này:xsd: danh sách các kiểu tùy chỉnh được tạo ra vào Danh sách <String>

<xsd:simpleType name="customId"> 
    <xsd:annotation> 
     <xsd:appinfo> 
      <jaxb:javaType name="com.company.identifiers.CustomId" parseMethod="fromString" printMethod="toString"/> 
     </xsd:appinfo> 
    </xsd:annotation> 
    <xsd:restriction base="xsd:int" /> 
</xsd:simpleType> 

Sau đó, tôi muốn có một danh sách các loại này trong một lớp Java được tạo ra:

<xsd:complexType name="SomeMessage"> 
    ... 
<xsd:attribute name="customIds" use="optional"> 
     <xsd:simpleType> 
      <xsd:list itemType="customId" /> 
     </xsd:simpleType> 
</xsd:attribute> 
    ... 
</xsd:complexType> 

Nhưng trường customIds, vì một số lý do, được tạo dưới dạng List<String>.

tôi đoán, xsd:sequence có thể được sử dụng thay cho xsd:list, nhưng SomeMessage đã có một xsd:choice, và như xa như tôi có được, đó là bất hợp pháp để có xsd:sequence trong khai báo tương tự.

Cảm ơn!

Trả lời

3

Mã được tạo bằng NetBeans 7.1.2, chạy trên Java 1.7.0_02.

Nếu bạn muốn ánh xạ loại đơn giản đến các lớp học Java, có một cách để làm là toàn cầu thiết lập mapSimpleTypeDef="true"

<xsd:annotation> 
    <xsd:appinfo> 
     <jaxb:globalBindings mapSimpleTypeDef="true"/> 
    </xsd:appinfo> 
</xsd:annotation> 

Tạo mã:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "SomeMessage") 
public class SomeMessage { 

    @XmlAttribute(name = "customIds") 
    protected List<CustomId> customIds; 

    /** 
    * Gets the value of the customIds property. 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link CustomId } 
    * 
    * 
    */ 
    public List<CustomId> getCustomIds() { 
     if (customIds == null) { 
      customIds = new ArrayList<CustomId>(); 
     } 
     return this.customIds; 
    } 

} 

Nếu bạn muốn tham khảo của bạn tồn tại trước đó Lớp CustomId, sau đó các công trình sau hoạt động trong trường hợp của tôi:

<xsd:simpleType name="customId"> 
    <xsd:restriction base="xsd:int"/> 
</xsd:simpleType> 
<xsd:complexType name="SomeMessage"> 
    <xsd:attribute name="customIds" use="optional"> 
     <xsd:simpleType> 
      <xsd:annotation> 
       <xsd:appinfo> 
        <jaxb:javaType name="java.util.List&lt;com.company.identifiers.CustomId>" parseMethod="Class1.fromString" printMethod="Class1.toString"/> 
       </xsd:appinfo> 
      </xsd:annotation> 
      <xsd:list itemType="customId"/> 
     </xsd:simpleType> 
    </xsd:attribute> 
</xsd:complexType> 

Bạn sẽ nhận được các foll do:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "SomeMessage") 
public class SomeMessage { 

    @XmlAttribute(name = "customIds") 
    @XmlJavaTypeAdapter(Adapter1 .class) 
    protected List<CustomId> customIds; 

    /** 
    * Gets the value of the customIds property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public List<CustomId> getCustomIds() { 
     return customIds; 
    } 

    /** 
    * Sets the value of the customIds property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setCustomIds(List<CustomId> value) { 
     this.customIds = value; 
    } 

} 

Và Adapter1 tạo:

public class Adapter1 
    extends XmlAdapter<String, List<CustomId>> 
{ 


    public List<CustomId> unmarshal(String value) { 
     return (Class1.fromString(value)); 
    } 

    public String marshal(List<CustomId> value) { 
     return (Class1.toString(value)); 
    } 

} 
+0

Cảm ơn bạn đã trả lời của bạn! Trên thực tế, tôi đang cố gắng để không nhận được một 'Danh sách ', nhưng một 'Danh sách '. Tôi đang sử dụng JAXB 2.2. – gregvonbautt

+0

Đối với 'xsd: sequence' và' xsd: choice', ý tôi là như sau: ' ... ' Đối với loại khai báo này, thông báo "Chuỗi phần tử" không hợp lệ, thất lạc hoặc xảy ra quá thường xuyên ". – gregvonbautt

+0

Tôi đã cập nhật để giải quyết các giải thích của bạn. Để có lựa chọn và chuỗi, bạn cần trình bao bọc trình tự như sau: ... .... Thuộc tính không thể được đặt bên trong một nhóm mô hình (chuỗi/lựa chọn/tất cả), chúng sống bên ngoài nó. Nếu bạn muốn sử dụng các phần tử thay vì một thuộc tính dựa trên một danh sách xsd:, thì bạn sẽ sử dụng trình tự lồng nhau trong ví dụ của tôi để thực hiện nó. –

Các vấn đề liên quan