2012-01-31 18 views
3

私はxjcを使っていくつかのクラスを作った。JAXB - List <Serializable>?

public class MyType { 

    @XmlElementRefs({ 
     @XmlElementRef(name = "MyInnerType", type = JAXBElement.class, required = false), 

    }) 
    @XmlMixed 
    protected List<Serializable> content; 

    public List<Serializable> getContent() { 
     if (content == null) { 
      content = new ArrayList<Serializable>(); 
     } 
     return this.content; 
    } 
} 

しかしMyInnerTypeがシリアライズではないので、私は

getContent().add(newItem); 

を使用して、内部要素を追加傾けます。 なぜオブジェクトリストではないのですか?内部要素を追加するにはどうすればよいですか?

+0

あなたはどのようなエラーを取得している。第二のリンクから

? – Tudor

+0

MyInnerTypeがシリアル化できません – bunnyjesse112

答えて

4

herehereをご覧ください(あなたのシナリオに確実に対応してください)。

<!-- schema fragment having mixed content --> 
<xs:complexType name="letterBody" mixed="true"> 
<xs:sequence> 
    <xs:element name="name" type="xs:string"/> 
    <xs:element name="quantity" type="xs:positiveInteger"/> 
    <xs:element name="productName" type="xs:string"/> 
    <!-- etc. --> 
</xs:sequence> 
</xs:complexType> 
<xs:element name="letterBody" type="letterBody"/> 


// Schema-derived Java code: 
// (Only annotations relevant to mixed content are shown below, 
// others are ommitted.) 
import java.math.BigInteger; 
public class ObjectFactory { 
    // element instance factories 
    JAXBElement<LetterBody> createLetterBody(LetterBody value); 
    JAXBElement<String>  createLetterBodyName(String value); 
    JAXBElement<BigInteger> createLetterBodyQuantity(BigInteger value); 
    JAXBElement<String>  createLetterBodyProductName(String value); 
    // type instance factory 
    LetterBody> createLetterBody(); 
} 

public class LetterBody { 
    // Mixed content can contain instances of Element classes 
    // Name, Quantity and ProductName. Text data is represented as 
    // java.util.String for text. 
    @XmlMixed 
    @XmlElementRefs({ 
      @XmlElementRef(name="productName", type=JAXBElement.class), 
      @XmlElementRef(name="quantity", type=JAXBElement.class), 
      @XmlElementRef(name="name", type=JAXBElement.class)}) 
    List getContent(){...} 
} 
+0

ありがとう!本当に私を助けました! – bunnyjesse112

+0

これは本当に助けになりました。リンクありがとう! –

+0

リンクが壊れています。新しいリンクをお願いしますか? – ulab

1

あなたはそこに追加する必要があると思いますか?私は同様の世代を使用しており、このようなフィールドを持っていて、それがStringコンテンツであることが期待されていました。

これはおそらく、これが生成されたxsdを表示するのに役立ちます。

+0

回答ありがとうございます。 MyInnerTypeは文字列ではありません。リストを生成し、リストを生成しないのはなぜですか? JAXBElementなどでMyInnerTypeをラップする必要がありますか? – bunnyjesse112

+1

あなたのオブジェクトは直列化できないので、それはいい考えです。しかし、なぜあなたのオブジェクトをシリアライズ可能にしないのですか?それはインタフェースを実装するだけの問題です。 – AHungerArtist