【问题标题】:Java XStream convert XML to ObjectJava XStream 将 XML 转换为对象
【发布时间】:2016-12-01 23:40:18
【问题描述】:

我正在使用XStream 将 XML 字符串转换为 Java 对象。

我有大量数据,但我在下面发布了最少的代码:

XStream xstream = new XStream(new StaxDriver());
xstream.alias("data", DetList.class);
xstream.alias("i", Details.class);
String s = new String("<data>\n"
           +"\t<i Name='asia' type='continent' id='11'></i>\n"
           +"\t<i Name='africa' type='continent' id='12'></i>\n"
           +"\t<i Name='japan' type='country' id='13'></i>\n"
           +"</data>");
System.out.println(s);
DetList data = (DetList) xstream.fromXML(s);

当我调试时,data 总是 null

这是我的 DetList 类:

public class DetList {
    private List<Details> detlist;

    public List<Details> getDetlist() {
        return detlist;
    }

    public void setDetlist(List<Details> detlist) {
        this.detlist = detlist;
    }
}

还有我的详细信息类:

public class Details {

    private String Name;
    private String type;
    private String id;

    //Getters and Setters are here.
}

data 为 null,应该包含 i 的列表。

我怎样才能让它工作?

【问题讨论】:

  • 你有什么错误吗?
  • @GOXR3PLUS 不,没有错误。我确定我错过了什么,但不确定是什么。
  • 你有name 变量的getter 和setter 方法吗?它也应该是name 而不是Name
  • 我这样做了,没有任何影响。
  • 看一下(x-stream.github.io/annotations-tutorial.html)如果你想使用@attributes,你的问题有多个错误。一是它期待items 而不是&lt;i&gt; &lt;/i&gt; 元素中的属性。

标签: java xstream


【解决方案1】:

如果您有兴趣,这是您的错误。我会在找到答案后立即更换它:

Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field application.DetList.i
---- Debugging information ----
message             : No such field application.DetList.i
field               : i
class               : application.DetList
required-type       : application.DetList
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /data/i
line number         : 2
version             : 1.4.9
-------------------------------
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.handleUnknownField(AbstractReflectionConverter.java:524)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:375)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:281)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1230)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1214)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1085)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1076)
    at application.Tester.main(Tester.java:15)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多