2

When I generate code from the following schema, two classes are created, A and B. But, these two classes are not annotated by @XmlRootElement, so I get an error when I try to marshal an instance of B.

I've looked on the web, I tried the solution using custom bindings (<xjc: simple/>) but it doesn't work. It seems that an element of type B must be defined in the schema in order for this solution to work.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns:ns="com:mycomp:service" xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="com:mycomp:service"
    elementFormDefault="qualified" xml:lang="EN">
    <complexType name="A" abstract="true">
        <sequence>
            <element name="a1" type="string" />
        <element name="a2" type="string" />
        </sequence>
    </complexType>
    <complexType name="B">
        <complexContent>
            <extension base="ns:A">
            <sequence>
                <element name="b1" type="string" />
            </sequence>
        </extension>
        </complexContent>
    </complexType>
</schema>

Thanks

4

3 に答える 3

1

スキーマはルート要素を宣言せず、型のみを宣言するため、正しいです。アノテーションが付けられたクラスのいずれかのインスタンスをマーシャリングする場合は、使用する要素名をマーシャラーに伝えるために@XmlType、そのインスタンスを でラップする必要があります。JAXBElement

// create context from the package name that contains your generated classes
JAXBContext ctx = JAXBContext.newInstance("com.mycomp.service");
Marshaller marshaller = ctx.createMarshaller();
marshaller.marshal(new JAXBElement<B>(new QName("com:mycomp:service", "someB"),
      B.class, instanceOfB), outputStream);

これは次のようなものを生成します

<someB xmlns="com.mycomp.service">
  <a1>foo</a1>
  <a2>bar</a2>
  <b1>baz</b1>
</someB>

編集: 自分でマーシャリングを行っているのではなく、代わりにマーシャリングする REST フレームワークのオブジェクトを返すとコメントしました。JAXBElement<B>の代わりに操作を返すと宣言することで、そこで同じトリックを使用できる場合がありますBが、他の回答で提案されているようにスキーマを変更する方がよいでしょう。

于 2012-10-11T22:12:54.397 に答える
1

XJC は、問題の最上位要素 (通常は単一のルート要素) の型が匿名の場合にのみ @XmlRootElement を生成します。

例えば。(未テスト) のようなもの:

<element name='B'>
   <complexType>
        <complexContent>
            <extension base="ns:A">
            <sequence>
                <element name="b1" type="string" />
            </sequence>
            </extension>
        </complexContent>
  </complexType>
</element>
于 2012-10-11T22:51:11.377 に答える
0

<element>スキーマにタグを追加する必要があります

<schema xmlns:ns="com:mycomp:service" xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="com:mycomp:service"
    elementFormDefault="qualified" xml:lang="EN">
    <complexType name="A" abstract="true">
        <sequence>
            <element name="a1" type="string" />
        <element name="a2" type="string" />
        </sequence>
    </complexType>
    <complexType name="B">
        <complexContent>
            <extension base="ns:A">
                <sequence>
                    <element name="b1" type="string" />
                </sequence>
            </extension>
        </complexContent>
    </complexType>
    <element name="A" type="ns:A"/>
    <element name="B" type="ns:B"/>
</schema>
于 2012-10-11T21:17:52.933 に答える