3

WS用に作成したXSDスキーマは次のとおりです

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:element name="shipmentStatus" type="shipmentStatusType" />

<xs:complexType name="shipmentStatusType">
    <xs:sequence>
        <xs:element name="orderNumber" type="xs:int"/>
    </xs:sequence>

    <xs:attribute name="requestStatus">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="SHIPPED"/>
                <xs:enumeration value="PENDING"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>

</xs:complexType>

JAXB 2.1 を使用して Java クラスを生成すると、shipmentStatusType という 1 つのクラスしか生成されませんでした。requestStatus を JAVA Enum として生成することを期待していましたが、そうではありませんでした。それは予想される動作ですか、それとも何か見逃しましたか?

4

2 に答える 2

4

列挙型/単純型宣言を最上位のものに抽出し、それを XML 属性の型として使用するだけです。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.example.com" xmlns="http://www.example.com"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    attributeFormDefault="unqualified">

    <xs:simpleType name="requestStatus">
        <xs:restriction base="xs:string">
            <xs:enumeration value="SHIPPED" />
            <xs:enumeration value="PENDING" />
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="shipmentStatus">
        <xs:sequence>
            <xs:element name="orderNumber" type="xs:int" />
        </xs:sequence>
        <xs:attribute name="requestStatus" type="requestStatus" />
    </xs:complexType>

    <xs:element name="shipmentStatus" type="shipmentStatus" />

</xs:schema>

それはあなたにそのような列挙型を与えるでしょう:

/**
 * <p>Java class for requestStatus.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * <p>
 * <pre>
 * &lt;simpleType name="requestStatus">
 *   &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string">
 *     &lt;enumeration value="SHIPPED"/>
 *     &lt;enumeration value="PENDING"/>
 *   &lt;/restriction>
 * &lt;/simpleType>
 * </pre>
 * 
 */
@XmlType(name = "requestStatus")
@XmlEnum
public enum RequestStatus {

    SHIPPED,
    PENDING;

    public String value() {
        return name();
    }

    public static RequestStatus fromValue(String v) {
        return valueOf(v);
    }

}

そしてそれを持つクラス:

/**
 * <p>Java class for shipmentStatus complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="shipmentStatus">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="orderNumber" type="{http://www.w3.org/2001/XMLSchema}int"/>
 *       &lt;/sequence>
 *       &lt;attribute name="requestStatus" type="{http://www.example.com}requestStatus" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "shipmentStatus", propOrder = {
    "orderNumber"
})
public class ShipmentStatus {

    protected int orderNumber;
    @XmlAttribute(name = "requestStatus")
    protected RequestStatus requestStatus;

    /**
     * Gets the value of the orderNumber property.
     * 
     */
    public int getOrderNumber() {
        return orderNumber;
    }

    /**
     * Sets the value of the orderNumber property.
     * 
     */
    public void setOrderNumber(int value) {
        this.orderNumber = value;
    }

    /**
     * Gets the value of the requestStatus property.
     * 
     * @return
     *     possible object is
     *     {@link RequestStatus }
     *     
     */
    public RequestStatus getRequestStatus() {
        return requestStatus;
    }

    /**
     * Sets the value of the requestStatus property.
     * 
     * @param value
     *     allowed object is
     *     {@link RequestStatus }
     *     
     */
    public void setRequestStatus(RequestStatus value) {
        this.requestStatus = value;
    }

}
于 2012-08-13T22:44:35.507 に答える
1

あなたはこのSO投稿と同じことを求めていると思います。この単純な型を列挙型にマップするには、カスタムバインディングファイルを作成する必要があります。

バインディングファイル:

<jaxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc" version="2.1"> 
    <jaxb:globalBindings generateIsSetMethod="true" fixedAttributeAsConstantProperty="true"> 
        <xjc:serializable/> 
    </jaxb:globalBindings> 
    <jaxb:bindings schemaLocation="file:/..../restricting-xml-attribute-to-enum-values.xsd"> 
        <jaxb:bindings node="//xs:complexType[@name='shipmentStatusType']/xs:attribute[@name='requestStatus']/xs:simpleType"> 
            <jaxb:typesafeEnumClass name="MyEnumType"/> 
        </jaxb:bindings> 
    </jaxb:bindings> 
</jaxb:bindings> 

生成されたクラス(関連する部分):

/**
 * <p>Java class for null.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * <p>
 * <pre>
 * &lt;simpleType>
 *   &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string">
 *     &lt;enumeration value="SHIPPED"/>
 *     &lt;enumeration value="PENDING"/>
 *   &lt;/restriction>
 * &lt;/simpleType>
 * </pre>
 * 
 */
@XmlType(name = "")
@XmlEnum
public enum MyEnumType {

    SHIPPED,
    PENDING;

    public String value() {
        return name();
    }

    public static ShipmentStatusType.MyEnumType fromValue(String v) {
        return valueOf(v);
    }

}
于 2012-08-13T20:40:24.003 に答える