簡體   English   中英

Xstream:如何使用數組反序列化Xml

[英]Xstream: How to deserialize Xml with array

我有一個帶有重復數組元素的xml,如何反序列化呢?

<root>
 <values>
   <val1>afa</val1>
   <val2>asgfasg</val2>
 </values>
 <values>
   <val1>hkjvlk</val1>
   <val2>sdsdgsdg</val2>
 </values>
 ...
</root>

 XStream xstream = new XStream(new DomDriver());
 xstream.alias("values", Inventory.class);
 InventoryResult inventory = (InventoryResult) xstream.fromXML(data.toString());

public class InventoryResult {
    private Inventory values;
}

我已經嘗試了List,Inventory [],但它們都沒有用。

首先,這只是整個xml doc的一部分嗎? 如是

<root>
    <values>
      <val1>afa</val1>
    </values>
    <values>
      <val2>hkjvlk</val2>
    </values>
  </root>

這是序列化的形式

values [] root = {new values("afa", null), new values(null, "hkjvlk")}; 

其中Class值看起來像這樣

class values{
    String val1;
    String val2;
    public values (String str, String str1){
        val1 = str;
        val2 = str1;                
    }
}

我建議編寫自己的轉換器: http//x-stream.github.io/converter-tutorial.html

暫無
暫無

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

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