i try to read multiple object with xStream
This is my XML file
<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>
With this code I can get onlY the first book, can you tell me how to read a code, with which i am able to read all objects(books)
XStream xstream = new XStream();
xstream.processAnnotations(Book.class);
Book a = (Book)xstream.fromXML(new File("a.xml"));
You can create a class Library:
public class Library {
public List<Book> books = new ArrayList<Book>();
}
and modify your xml to fill that data:
<library>
<books>
<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>
</books>
</library>
And in your main:
public static void main(final String[] args) {
final String xmlInput = "pathToYourFile";
try {
final XStream xstream = new XStream();
xstream.alias("library", Library.class);
xstream.alias("book", Book.class);
final Library a = (Library) xstream.fromXML(new File(xmlInput));
System.out.println(a);
} catch (final Exception e) {
e.printStackTrace();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.