与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
最满意答案
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 ofa
, 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 dod[i][j]-=[0,0]
.You should replace
d=[a,b,c]
byd = [list(a), b, c]
相关问答
更多-
好的,我听说它也可以通过readline来完成。 您可以import readline并在配置中将所需的键(Shift + Enter)设置为一个宏,该宏会将一些特殊字符置于行和换行符的末尾。 然后你可以在循环中调用raw_input 。 喜欢这个: import readline # I am using Ctrl+K to insert line break # (dont know what symbol is for shift+enter) readline.parse_and_bind ...
-
为什么Python的列表没有shift / unshift方法?(Why Python's list not have a shift/unshift methods? [closed])[2023-05-03]
Python列表针对快速固定长度操作进行了优化,并为pop(0)和insert(0, v)操作产生O(n)内存移动成本,这些操作改变了底层数据表示的大小和位置。 你可能会感兴趣的是collections.deque ,它是一个类似列表的容器,在两端都有快速附加和弹出。 Deques支持线程安全,高效的内存追加,并从双侧出现,并且在任一方向都具有大致相同的O(1)性能。 你似乎在问的关于缺失的方法是以名称appendleft和popleft : appendleft(x) Add x to the left ... -
一个collections.deque是优化的拉和推两端。 他们甚至有一个专用的rotate()方法。 from collections import deque items = deque([1, 2]) items.append(3) # deque == [1, 2, 3] items.rotate(1) # The deque is now: [3, 1, 2] items.rotate(-1) # Returns deque to original state: [1, 2, 3] item = ...
-
你只需要向X添加一行,全部为1。 you only need to add a row to X that is all 1.
-
如何在python中使用@以及@property和@classmethod(how to use @ in python.. and the @property and the @classmethod)[2021-10-30]
装饰器需要是一个可调用对象(实现__call__的函数或对象),其中参数是已装饰的函数,并且结果是将替换已装饰的函数的函数,因此,要使用你打印'sss'而不是打印'aaa'的例子: >>> def a(f): ... def replacementfunc(): ... print 'sss' ... return replacementfunc; ... >>> @a ... def b(): ... print 'aaa' ... >>> b() sss ... -
如果你的出身意味着矩阵的第一个元素,你已经很好。 如果你想以另一种方式转动它,只需用t()来翻转它。 如果要将其翻转为矩阵形式(即左上角的第一个元素),请像第二个示例中那样翻转第一个维度。 a <- matrix(1:16, 4, 4) image(t(a)) image(t(a[4:1,])) If you by origin mean the first element of the matrix you're already good. Just flip it with t() if you wa ...
-
Python中的文本移位功能(Text Shift function in Python)[2022-08-12]
看起来你在做cesar-cipher加密,所以你可以尝试这样的事情: strs = 'abcdefghijklmnopqrstuvwxyz' #use a string like this, instead of ord() def shifttext(shift): inp = raw_input('Input text here: ') data = [] for i in inp: #iterate over the text ... -
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. Th ...
-
你可以使用切片: def leftshift(ba, count): return ba[count:] + (bitarray('0') * count) def rightshift(ba, count): return (bitarray('0') * count) + ba[:-count] 它们保持输入的位宽,一端丢弃位,另一端填充0 。 您可以创建自己的bitarray类型的子类: class mybitarray(bitarray): def __lshift__( ...
-
我找到了另一种get_root_coords window = self.get_window() x,y = window.get_root_coords(0, 0) 它甚至可以在多个屏幕上工作,我可以将我的Gtk.Window移动到我的按钮旁边。 I found an alternative get_root_coords window = self.get_window() x,y = window.get_root_coords(0, 0) It works even with multiple ...