gpt4 book ai didi

java - XStream arrayList 到 XML 和从 XML

转载 作者:行者123 更新时间:2023-12-04 14:42:02 25 4
gpt4 key购买 nike

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

我发现了这个,并尝试以同样的方式进行操作:How to convert List of Object to XML doc using XStream

可惜我失败了。这是我到目前为止所拥有的:包含 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)
(...)

我不明白为什么会出现转换异常,对我来说,如果发生异常时出现这一行,一切似乎都是正确的。我不知道出了什么问题,请帮忙。

最佳答案

你忘了加

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

加载时也是如此。

关于java - XStream arrayList 到 XML 和从 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24016334/

25 4 0