2016-05-23 7 views
1

List<Serializabe> conentに要素を追加する方法はわかりません.JaxBを使って自動生成されたJavaクラスから取得しました。List <Serializable>の内容を要素に追加する方法JaxB?

例えば、私はそのリスト内の単純な文字列を追加する必要がありますが、私は、文字列

sadrzaj.getContent().add("some string"); 

を渡すときには、その

java.lang.ClassCastExceptionが言う:jaxb.from.xsd .Clan $ Sadrzaj $ STAVはここjava.lang.Stringで

にキャストすることができない私のコードです:

静的クラスSadrzajのための210
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "content" 
}) 
public static class Sadrzaj { 

@XmlElementRefs({ 
@XmlElementRef(name = "Tekst", namespace = "http://www.parlament.gov.rs/clan", type = JAXBElement.class), 
@XmlElementRef(name = "Stav", namespace = "http://www.parlament.gov.rs/clan", type = JAXBElement.class) 
    }) 
    @XmlMixed 
    protected List<Serializable> content; 

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

私のXMLスキーマは、次のようになります

&lt;complexType> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;choice> 
*   &lt;element name="Stav" maxOccurs="unbounded"> 
*   &lt;complexType> 
*    &lt;complexContent> 
*    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*     &lt;sequence> 
*     &lt;element name="Redni_broj" type="{http://www.w3.org/2001/XMLSchema}long"/> 
*     &lt;element name="Tekst" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*     &lt;/sequence> 
*    &lt;/restriction> 
*    &lt;/complexContent> 
*   &lt;/complexType> 
*   &lt;/element> 
*   &lt;element name="Tekst" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded"/> 
*  &lt;/choice> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 

答えて

0

この関連記事に見てください:JAXB - List?および関連Java8のマニュアルを:Annotation Type XmlMixed

、シリアライズJAXBElementのを作成するには、生成されたObjectFactoryを使用する必要があります。

LetterBody lb = ObjectFactory.createLetterBody(); 
JAXBElement<LetterBody> lbe = ObjectFactory.createLetterBody(lb); 
List gcl = lb.getContent(); //add mixed content to general content property. 
gcl.add("Dear Mr."); // add text information item as a String. 

// add child element information item 
gcl.add(ObjectFactory.createLetterBodyName("Robert Smith")); 
gcl.add("Your order of "); // add text information item as a String 

// add children element information items 
gcl.add(ObjectFactory. 
         createLetterBodyQuantity(new BigInteger("1"))); 
gcl.add(ObjectFactory.createLetterBodyProductName("Baby Monitor")); 
gcl.add("shipped from our warehouse"); // add text information item 
関連する問題