簡體   English   中英

使用Jaxb解組通用子級

[英]Unmarshalling generic child using Jaxb

我有一個xml文件,格式為:-

<item>
    <item_attribute index="1" type="1" >
        <name>value1</name>
    </item_attribute>
    <item_attribute index="2" type="1" >
        <a_differnt_name>value2</a_different_name>
    </item_attribute>
    <item_attribute index="5" type="2" >
        <another_name>value3</another_name>
    </item_attribute>
</item>

我正在使用JAXB解組xml,並為除“ item_attribute”的孩子之外的每個元素設置了一個類。 我想一般地在每個“ item_attribute”元素中解組數據(元素名稱和元素值),而不知道該元素被稱為什么。

我所知道的是,“ item_attribute”始終只有1個子元素,該子元素可以被調用並包含任何內容。

我嘗試使用:

public class Item_attribute {

    private int index;
    private Object data;

    @XmlAttribute(name = "index")
    public int getIndex() {
        return index;
    }
    public void setIndex(int index) {
        this.index = index;
    }

    @XmlAnyElement(lax = true)
    public Object getData() {
        return this.data;
    }

}

但它總是拋出一個非法注釋異常!

如果注釋字段(實例變量),則需要添加以下類型級別注釋:

@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    @XmlAnyElement(lax = true)
    private Object data;

     public Object getData() {  
          return this.data;
     }

}

或者,您可以將注釋放在get方法上。

public class Foo {

    private Object data;

     @XmlAnyElement(lax=true)
     public Object getData() {  
          return this.data;
     }

}

將@XmlAnyElement(lax = true)添加到每個錯誤中(javax.xml.bind.JAXBElement沒有默認的構造函數)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM