Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XStream arrayList与XML之间的转换_Java_Xml_Arraylist_Xstream - Fatal编程技术网

Java XStream arrayList与XML之间的转换

Java XStream arrayList与XML之间的转换,java,xml,arraylist,xstream,Java,Xml,Arraylist,Xstream,我不知道现在的问题在哪里。第一次使用xml时,我在将ArrayList放入xml文件并从中提取时遇到了一些问题 我发现了这个,我试着用同样的方法: 但不幸的是我失败了。 以下是我迄今为止的情况: 包含ArrayList的类: public class ElbowList{ private ArrayList<Elbow> elbows = new ArrayList<>(); public ElbowList(){ elbows = new ArrayList

我不知道现在的问题在哪里。第一次使用xml时,我在将ArrayList放入xml文件并从中提取时遇到了一些问题

我发现了这个,我试着用同样的方法:

但不幸的是我失败了。 以下是我迄今为止的情况: 包含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文件时,我得到了一个异常调用:

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);
            }
我不明白为什么会有转换异常,对我来说,当异常发生时,如果出现这一行,一切似乎都是正确的。我不知道出了什么问题,请帮忙。

你忘记添加了

XStream xstream = new XStream();
xstream.alias("elbow", Elbow.class);
xstream.alias("elbows", ElbowList.class);
xstream.addImplicitCollection(ElbowList.class, "elbows", Elbow.class);

加载时也是如此。

谢谢。就是它。我没有忘记,我不知道我必须这样做。
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)
(...)
XStream xstream = new XStream();
xstream.alias("elbow", Elbow.class);
xstream.alias("elbows", ElbowList.class);
xstream.addImplicitCollection(ElbowList.class, "elbows", Elbow.class);