3

生成されたクラスをラップするには、classImplバインディングを使用しますが、生成されたクラスのコレクションは、classImplのタイプではなく、生成されたタイプを返します。もちろん、classImplのリストが必要です...

私のxsd:

<complexType name="A">
<xs:sequence>
    <element name="listB" type="sbs:B" minOccurs="0" maxOccurs="unbounded"></element>
    <element name="singleB" type="sbs:B" minOccurs="1" maxOccurs="1"></element>
</xs:sequence>
</complexType>
<complexType name="B">
<xs:annotation><xs:appinfo>
    <jxb:class implClass="BWrapper" />
</xs:appinfo></xs:annotation>
</complexType>

生成されるクラスは次のとおりです。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "A", propOrder = {
    "listB",
    "singleB"
})
public class A {
    @XmlElement(type = BWrapper.class)
    protected List<B> listB;
    @XmlElement(required = true, type = BWrapper.class)
    protected BWrapper singleB;

予想通り、singleBはBWrapperと入力されているので、なぜlistBがBWrapperのリストではなくBのリストであるのですか?

よろしくお願いします!!

4

2 に答える 2

2

タイプ が BWrapper によって実装できることを定義しました。要素listB が BWrapper を参照する必要があることを明示的に指定する必要があります。

このインラインをスキーマに設定する方法がわからなかったため、外部の .xjb ファイルを使用する必要がありました。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <!-- bindings in the scope of the schema -->
    <jaxb:bindings schemaLocation="./Test.xsd" node="/xs:schema">

        <!-- apply bindings in the scope of the complex type B. -->
        <jaxb:bindings node="//xs:complexType[@name='B']">
            <!-- the java BWrapper extends the B object created by XJC -->
            <jaxb:class implClass="com.foobar.BWrapper"/>
        </jaxb:bindings>

        <!-- specify bindings in the scope of the element 'listB' within -->
        <!-- the the complex type A -->
        <jaxb:bindings node="//xs:complexType[@name='A']//xs:element[@name='listB']">
            <!-- the element should reference the BWrapper cLass -->
            <jaxb:class ref="com.foobar.BWrapper"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

これにより、次が生成されます。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "A", propOrder = {
    "listB",
    "singleB"
})
public class A {
    protected List<com.foobar.BWrapper> listB;
    @XmlElement(required = true, type = com.foobar.BWrapper.class)
    protected com.foobar.BWrapper singleB;

また、listB の getter は BWrappers のリストを返します。単一のアイテムとリストの間にこの矛盾がある理由はわかりませんが、少なくともこれは機能します。

于 2016-01-13T05:09:44.867 に答える
0

typeプロパティは、@XmlElementこのユース ケースの JAXB 実装 (Metro、MOXy、JaxMe など) を構成する正しい方法です。XmlAccessorTypeクラスに注釈を追加しても問題は発生しますか?

@XmlAccessorType(XmlAccessType.FIELD)
public class A {
    @XmlElement(type = BWrapper.class)
    protected List<B> listB;
    @XmlElement(required = true, type = BWrapper.class)
    protected BWrapper singleB;
}

例については、次を参照してください。

于 2011-06-16T14:12:42.050 に答える