使JAXB XJC为简单类型生成包装类

时间:2012-10-04 06:29:40

标签: java jaxb xjc

我有以下xml类型:

<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

3 个答案:

答案 0 :(得分: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 :(得分:1)

您的FaxNumbers xsd定义中的

而不是行<xsd:element name="FaxNumber" type="FaxNumber" maxOccurs="unbounded" />使用<xsd:element name="FaxNumber" ref="FaxNumber" maxOccurs="unbounded" />。我认为这是你的问题

答案 2 :(得分: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;
    }

}