gpt4 book ai didi

java - 使用 X-Stream 在 Android 中反序列化 XML

转载 作者:行者123 更新时间:2023-12-01 04:49:09 26 4
gpt4 key购买 nike

已解决将内容内容更改为:

// 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 的方式,它无需内容即可工作。

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

最后我的对象:

项目.cs

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

@XStreamImplicit(itemFieldName = "Item")
public List<Item> items = new ArrayList<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>();
}

内容.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;
}

我不太确定我对下一个级别的注释是否正确。如有任何帮助,我们将不胜感激。

最佳答案

通过将 Contents 更改为列表并向其中添加 XStream marshall 来解决此问题。

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

关于java - 使用 X-Stream 在 Android 中反序列化 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15274435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com