Taranfx Taranfx - 3 months ago 15
Java Question

Xstream: How to deserialize Xml with array

I've an xml with a repeating array element, how do I deserialize it?

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


I've tried List, Inventory[], but none of them work.

Answer

First of all is this only a part of entire xml doc? if yes

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

This is the serialized form of

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

where Class values will look like this

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