2012-10-04 59 views
8

我有以下的XML類型:讓JAXB XJC產生的包裝類簡單類型

<xsd:element name="FaxNumbers" minOccurs="0"> 
    <xsd:complexType> 
     <xsd:sequence> 
      <xsd:element name="FaxNumber" type="FaxNumber" maxOccurs="unbounded" /> 
     </xsd:sequence> 
    </xsd:complexType> 
</xsd:element> 

的FaxNumber類型如下:

<xsd:simpleType name="FaxNumber"> 
    <xsd:restriction base="xsd:string"> 
     <xsd:minLength value="1" /> 
    </xsd:restriction> 
</xsd:simpleType> 

生成的XML應該是這樣的:

<FaxNumbers> 
    <FaxNumber>3878374</FaxNumber> 
    <FaxNumber>387833434</FaxNumber> 
    <FaxNumber>3878334</FaxNumber> 
</FaxNumbers> 

當運行JAXB XJC從XSD生成Java類時,它會生成以下類:

@XmlType(name = "FaxNumbers") 
public class FaxNumbers { 

    /** 
    * No comment. 
    */ 
    @XmlElement(name = "FaxNumber", required = true) 
    protected List<String> faxNumber = new ArrayList<String>(); 

} 

但是我想綁定FaxNumber複合類是這樣的:

@XmlType(name = "FaxNumbers") 
public class FaxNumbers { 

    /** 
    * No comment. 
    */ 
    @XmlElement(name = "FaxNumber", required = true) 
    protected List<FaxNumber> faxNumber = new ArrayList<String>(); 

} 

Where FaxNumber class looks like this: 

public class FaxNumber{ 

    @XmlValue 
    private String value; 
} 

有沒有辦法來定義這樣的JAXB綁定XML綁定?

注:不幸的是,我沒有控制權,不能更改XSD

回答

6

我不認爲你可以得到XJC產生這樣爲你FaxNumber類,但你可以自己寫,然後使用外部綁定文件將其插入到正確的位置。

package com.example; 
public class FaxNumber { 
    private String value; 
    // getter/setter 

    public static FaxNumber parse(String s) { 
    FaxNumber n = new FaxNumber(); 
    n.value = s; 
    return n; 
    } 

    public static String print(FaxNumber n) { 
    return n.value; 
    } 
} 

綁定:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      jaxb:version="2.0"> 
    <jaxb:bindings schemaLocation="example.xsd"> 
    <jaxb:bindings node="//xsd:simpleType[@name='FaxNumber']"> 
     <jaxb:javaType name="com.example.FaxNumber" 
      parseMethod="com.example.FaxNumber.parse" 
      printMethod="com.example.FaxNumber.print"/> 
    </jaxb:bindings> 
    </jaxb:bindings> 
<jaxb:bindings> 
1

,而不是線<xsd:element name="FaxNumber" type="FaxNumber" maxOccurs="unbounded" />在FaxNumbers XSD定義使用<xsd:element name="FaxNumber" ref="FaxNumber" maxOccurs="unbounded" />。我認爲這是你的問題

+0

不幸的是,我不控制和不能改變的XSD(將更新原帖)。 –

+0

那麼我恐怕你在使用它之前被卡住了手動修改生成的java文件。如果您的情況可能 –

+0

手動編輯生成的java文件可以正常工作。但是,當XSD更改並且我預先生成文件時,我必須每次手動執行更新。 –

1

以下作品適合我。希望有所幫助!

XSD

<xsd:schema xmlns="http://www.hcentive.com/ws/eligibility/schema/beans" 
    targetNamespace="http://www.hcentive.com/ws/eligibility/schema/beans" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<xsd:element name="FaxNumbers"> 
    <xsd:complexType> 
     <xsd:sequence> 
      <xsd:element name="FaxNumber" type="FaxNumber" maxOccurs="unbounded" /> 
     </xsd:sequence> 
    </xsd:complexType> 
</xsd:element> 
<xsd:complexType name="FaxNumber"> 
    <xsd:sequence> 
     <xsd:element type="xsd:string" name="value" /> 
    </xsd:sequence> 
</xsd:complexType> 
</xsd:schema> 

生成的對象:FaxNumbers.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "faxNumber" 
}) 
@XmlRootElement(name = "FaxNumbers") 
public class FaxNumbers { 

    @XmlElement(name = "FaxNumber", required = true) 
    protected List<FaxNumber> faxNumber; 

    /** 
    * Gets the value of the faxNumber property. 
    * 
    * <p> 
    * This accessor method returns a reference to the live list, 
    * not a snapshot. Therefore any modification you make to the 
    * returned list will be present inside the JAXB object. 
    * This is why there is not a <CODE>set</CODE> method for the faxNumber property. 
    * 
    * <p> 
    * For example, to add a new item, do as follows: 
    * <pre> 
    * getFaxNumber().add(newItem); 
    * </pre> 
    * 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link FaxNumber } 
    * 
    * 
    */ 
    public List<FaxNumber> getFaxNumber() { 
     if (faxNumber == null) { 
      faxNumber = new ArrayList<FaxNumber>(); 
     } 
     return this.faxNumber; 
    } 

} 
+1

正如我在問題中發佈的那樣,我無法修改XSD。 –

+0

那麼你可以修改什麼?你只是從XSD生成Java類吧? – Anshu

+0

這正是我正在做的。 –

相關問題