簡體   English   中英

將內聯元素列表直接反序列化為List

[英]Deserialize inline elementlist directly into a List

我有以下XML結構:

<keys>
   <key>
      <tag>
         someValue
      </tag>
   </key>
   <key>
      <tag>
         someValue
      </tag>
   </key>
</keys>

key元素在以下類的代碼中表示:

public class Key {

    @Element(name = "tag")
    private String tag;
}

我的目標是將這些直接反序列化為List ,如下所示:

Serializer serializer = new Persister();
List<Key> list = serializer.read(Key.class, inputStream); // pseudo code

我如何用Simple實現這一目標?

像這樣使用@ElementList注釋

@Root(name="keys")
public class KeyList {

    @ElementList(inline=true, entry="key")
    private List<Key> keys;

    public List<Key> getKeys() {
       return keys;
    }
}

然后

Persister persister = new Persister();
List<Key> keys = persister.read(KeyList.class, source).getKeys();

我建議使用JAXB(Java Architecture for XML Binding)

這里有幾個教程的鏈接:

他們可以比我更好地解釋它。 但基本上你使用javax.xml.bind.annotation包中定義的注釋來定義你的xml結構。 然后創建一個簡單的JAXB Handler類來處理編組和解組。

暫無
暫無

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

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