首页 \ 问答 \ 与XML有关的XStream arrayList(XStream arrayList to and from XML)

与XML有关的XStream arrayList(XStream arrayList to and from XML)

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

我找到了这个,我试着这样做: 如何使用XStream将对象列表转换为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:

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文件,我有一个异常调用的地方:

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 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.

I was found this and I tried to do it same way: How to convert List of Object to XML doc using XStream

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

Save to 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);
            }

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

An exception I get:

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.


原文:https://stackoverflow.com/questions/24016334
更新时间:2023-03-15 08:03

最满意答案

d包含一个引用,而不是副本。

在循环的第一次迭代期间从[3,4]改变为[0,0] 。 这就是为什么,循环的下一个迭代将执行d[i][j]-=[0,0]

你应该用d=[a,b,c] d = [list(a), b, c]代替d=[a,b,c] d = [list(a), b, c]


d contains a reference of a, not a copy.

a changes from [3,4] to [0,0] during the first iteration of the loop. That's why, the next iteration of the loop will do d[i][j]-=[0,0].

You should replace d=[a,b,c] by d = [list(a), b, c]

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。