Search code examples
javaandroidxstream

Deserializing XML in Android using X-Stream


SOLVED Changed Contents contents to:

// Lists
    @XStreamImplicit(itemFieldName = "Contents")
    public List<Contents> Contents = new ArrayList<Contents>();

I'm trying to deserialize the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<Items>
   <Item>
      <type>Test</type>
      <uid>1</uid>
      <depends>1</depends>
      <catid>1</catid>
      <label>Test</label>
      <defaultVal>default</defaultVal>
      <answer>Test</answer>
      <Contents>
         <Content>
            <uid>1</uid>
            <label>Test</label>
         </Content>
      </Contents>
   </Item>
</Items>

The problem is that when I reach Contents it crashes due to going deeper by another level.

This is how i DeSerialize my XML, it works without the contents.

public static Object DeSerializeFromXML(String xml, Class items, Class item)
{
    XStream xstream = new XStream();
    xstream.processAnnotations(new Class[] { items, item });

    return xstream.fromXML(xml);
}

Here's how I call the DeSerialize method:

Items reader = (Items)Data.Serialization.Instance().DeSerializeFromXML(xml, Items.class, Item.class);

And as last my Objects:

Items.cs

@XStreamAlias("Items")
public class Items
{
    public Items() { items = new ArrayList<Item>(); }

    @XStreamImplicit(itemFieldName = "Item")
    public List<Item> items = new ArrayList<Item>();
}

Item.cs

@XStreamAlias("Item")
public class Item
{
    // Type
    public String type = "";

    // Ids
    public int uid = 0;
    public int catid = 0;
    public int depends = 0;

    // Values
    public String label = "";
    public String defaultVal = "";
    public String answer = "";

    // Lists
    public Contents content;    

    public Item()
    {
    }

    public Item(String type, int uid, int catid, int depends, String label,
            String defaultval, String answer)
    {
        this.type = type;
        this.uid = uid;
        this.catid = catid;
        this.depends = depends;
        this.label = label;
        this.defaultVal = defaultval;
        this.answer = answer;

        content = new Contents();
    }

    @XStreamAlias("Contents")
    public static class Contents
    {
        public Contents()
        {
            contents = new ArrayList<Content>();
        }

        @XStreamImplicit(itemFieldName = "Content")
        public List<Content> contents = new ArrayList<Content>();
    }

Content.cs

@XStreamAlias("Content")
public class Content
{
    public int uid = 0;
    public String label = "";

    public Content()
    {
    }

    public Content(int uid, String label)
    {
        this.uid = uid;
        this.label = label;
    }

I'm not quite sure if my annotations for the next level are correct. Any help is appreciated.


Solution

  • Solved it by changing Contents to a list and adding the XStream marshall to it.

    @XStreamImplicit(itemFieldName = "Contents")
    public List<Contents> Contents = new ArrayList<Contents>();