解决 将内容更改为:
// Lists
@XStreamImplicit(itemFieldName = "Contents")
public List<Contents> Contents = new ArrayList<Contents>();
我正在尝试反序列化以下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>
问题在于,当我到达内容时,它会因为更深层次而崩溃。
这就是我对我的XML进行DeSerialize的方式,它没有内容。
public static Object DeSerializeFromXML(String xml, Class items, Class item)
{
XStream xstream = new XStream();
xstream.processAnnotations(new Class[] { items, item });
return xstream.fromXML(xml);
}
以下是我如何调用DeSerialize方法:
Items reader = (Items)Data.Serialization.Instance().DeSerializeFromXML(xml, Items.class, Item.class);
最后我的对象:
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;
}
我不太确定我的下一级注释是否正确。 任何帮助表示赞赏。
答案 0 :(得分:0)
通过将内容更改为列表并将XStream marshall添加到其中来解决此问题。
@XStreamImplicit(itemFieldName = "Contents")
public List<Contents> Contents = new ArrayList<Contents>();