2016-12-01 297 views
1

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

我有一个巨大的数据集,但我张贴下面的最少的代码:

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

当我调试,数据总是

这里是我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. 
} 

数据为null,这是应该包含列表。

我怎样才能使它工作?

+0

您是否有任何错误? – GOXR3PLUS

+0

@ GOXR3PLUS不,没有错误。我敢肯定,我错过了一些东西,但不知道是什么。 –

+0

你有'name'变量的getter和setter?它也应该是名称'Name'的名称'' – GOXR3PLUS

回答

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)