0

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.

Taranfx
  • 10,361
  • 17
  • 77
  • 95
  • How can two variables have same name as array? Don't get it :( Where did you get the xml from, what is the source? XSD? – titogeo Apr 11 '12 at 19:16
  • sorry for the confusion, the tag names are different. Updated the xml above. Source of xml is some legacy app that doesn't even have an xsd. – Taranfx Apr 12 '12 at 03:20

2 Answers2

1

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;                
    }
}
titogeo
  • 2,156
  • 2
  • 24
  • 41
1

I would advice to write your own Converter: http://x-stream.github.io/converter-tutorial.html

facundofarias
  • 2,973
  • 28
  • 27
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211