XStream的ArrayList的与XML [英] XStream arrayList to and from XML
问题描述
我不知道是哪里的问题的时刻。第一次使用XML和我有一些问题将在ArrayList的XML文件,并从中服用。
I do not know where the problem is at the moment. First time using xml, and I got some problems with putting ArrayList in xml file and taking it from it.
我发现这一点,我试图做同样的方式:<一href=\"http://stackoverflow.com/questions/4077071/how-to-convert-list-of-object-to-xml-doc-using-xstream\">How使用的XStream 以列表转换对象到XML文档
I was found this and I tried to do it same way: How to convert List of Object to XML doc using XStream
但不幸的是我失败了。
这里是我到目前为止有:
持有ArrayList类:
but unfortunately I failed. Here what I have so far: Class that holds ArrayList:
public class ElbowList{
private ArrayList<Elbow> elbows = new ArrayList<>();
public ElbowList(){
elbows = new ArrayList<Elbow>();
}
public void setElbows(ArrayList<Elbow> elbows){
this.elbows.clear();
this.elbows = elbows;
}
public ArrayList<Elbow> getElbows() {
return elbows;
}
public void add(Elbow elbow){
elbows.add(elbow);
}
}
保存到XML:
MainFrame mainFrame = (MainFrame) SwingUtilities.getWindowAncestor(SetupPanel.this);
ElbowList elbowList = (ElbowList) mainFrame.getObjects().get(2); //get ElbowList object
XStream xstream = new XStream();
xstream.alias("elbow", Elbow.class);
xstream.alias("elbows", ElbowList.class);
xstream.addImplicitCollection(ElbowList.class, "elbows", Elbow.class);
String xml = xstream.toXML(elbowList.getElbows());
System.out.println(xml);
try {
PrintWriter out = new PrintWriter("Save.xml");
out.println(xml);
out.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(SetupPanel.class.getName()).log(Level.SEVERE, null, ex);
}
和一个以上的实际上似乎正常工作。
加载XML文件中,有在那里我得到一个例外电话:
And that one above actually seems to work correctly. Load XML file, there is where I got an exception call:
try {
XStream xstream = new XStream();
FileReader reader = new FileReader("Save.xml");
MainFrame mainFrame = (MainFrame) SwingUtilities.getWindowAncestor(SetupPanel.this);
ElbowList elbowList = (ElbowList) mainFrame.getObjects().get(2);
elbowList.setElbows((ArrayList<Elbow>) xstream.fromXML(reader));//exception occurs here
SpacePanel spacePanel = (SpacePanel) mainFrame.getObjects().get(1);
spacePanel.repaint();
} catch (IOException ex) {
Logger.getLogger(SetupPanel.class.getName()).log(Level.SEVERE, null, ex);
}
这是例外,我得到:
Exception in thread "AWT-EventQueue-0" com.thoughtworks.xstream.converters.ConversionException: elbow : elbow
---- Debugging information ----
message : elbow
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : elbow
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /list/elbow
line number : 2
version : 1.4.7
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
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:1185)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1169)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1040)
at view.SetupPanel$2.actionPerformed(SetupPanel.java:78)
(...)
我不明白为什么有一个转换的异常,对我来说一切似乎权利,如果当异常发生时谈到此行。我没有更多的想法什么是错,请帮忙。
I dont get why there is an conversion exception, for me everything seems right if it comes to this line when exception occurs. I have no more ideas whats wrong, please help.
推荐答案
您忘了补充
XStream xstream = new XStream();
xstream.alias("elbow", Elbow.class);
xstream.alias("elbows", ElbowList.class);
xstream.addImplicitCollection(ElbowList.class, "elbows", Elbow.class);
太,加载时。
这篇关于XStream的ArrayList的与XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!