9

我有以下 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

4

3 回答 3

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>
于 2012-10-04T08:28:44.780 回答
1

而不是<xsd:element name="FaxNumber" type="FaxNumber" maxOccurs="unbounded" />在您的 FaxNumbers xsd 定义中使用<xsd:element name="FaxNumber" ref="FaxNumber" maxOccurs="unbounded" />. 我认为这是你的问题

于 2012-10-04T06:41:08.903 回答
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;
    }

}
于 2012-10-04T06:46:56.990 回答