【发布时间】:2015-02-09 10:39:01
【问题描述】:
我尝试使用 xStream 读取多个对象
这是我的 XML 文件
<book>
<title>abc</title>
<author>A</author>
<pagesCount>0</pagesCount>
</book><book>
<title>qwe</title>
<author>B</author>
<pagesCount>0</pagesCount>
</book><book>
<title>zxc</title>
<author>C</author>
<pagesCount>0</pagesCount>
</book>
用这个代码我只能得到第一本书,你能告诉我如何阅读一个代码,我可以用它阅读所有对象(书籍)
XStream xstream = new XStream();
xstream.processAnnotations(Book.class);
Book a = (Book)xstream.fromXML(new File("a.xml"));
【问题讨论】:
-
这个文件是你生成的吗?我希望有一个“外部”标签,可能是
<books>,然后您会将 xml 反序列化为一个包含书籍对象列表的类。 -
我使用以下代码创建此文件:`FileOutputStream file = new FileOutputStream("a.xml", true); PrintWriter print = new PrintWriter(file); print.write(xstream.toXML(object));
标签: java xml deserialization xstream xml-deserialization