【问题标题】:How to Add Element in List<Serializable> content JaxB?如何在 List<Serializable> 内容 JaxB 中添加元素?
【发布时间】:2016-09-20 21:48:41
【问题描述】:

我不知道如何将元素添加到 List&lt;Serializabe&gt; 内容,这是我从使用 JaxB 自动生成的 java 类中获得的。

例如,我需要在该列表中添加简单的字符串,但是当我传递一个字符串时

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

它说

java.lang.ClassCastException: jaxb.from.xsd.Clan$Sadrzaj$Stav 无法转换为 java.lang.String

这是我的代码:

@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;
    }

我的静态类 Sadrzaj 的 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>

【问题讨论】:

    标签: java xml list jaxb serializable


    【解决方案1】:

    请查看此相关帖子: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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多