0

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"));
alle3x
  • 475
  • 2
  • 6
  • 18
  • Did you generate this file? I'd be expecting an "outer" tag, perhaps `` to be present, you'd then be deserializing the xml into a class which contains a list of book objects. – Davie Brown Feb 09 '15 at 10:52
  • I create this file using this code : `FileOutputStream file = new FileOutputStream("a.xml", true); PrintWriter print = new PrintWriter(file); print.write(xstream.toXML(object)); – alle3x Feb 09 '15 at 10:54

1 Answers1

1

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

}
jjlema
  • 850
  • 5
  • 8
  • can you explain me please what i have to do, because i work with xStream from 5 hours and i don`t have a good view at it. Thanks – alle3x Feb 09 '15 at 10:57
  • okey i understand everything, just one simple question how to rewrite my code in the way it will add `library` , my code to write is FileOutputStream file = new FileOutputStream("a.xml", true); PrintWriter print = new PrintWriter(file); print.write(xstream.toXML(object)); – alle3x Feb 09 '15 at 11:14
  • @user3613534 I have edited the answer adding the main method, the only thing you have to add are the alias. – jjlema Feb 09 '15 at 11:34
  • yes, but in this way it work only if i go to my xml file and manually write it doesn`t at it automatic – alle3x Feb 09 '15 at 11:38
  • @user3613534 you can do it manually or using code http://stackoverflow.com/questions/13741751/modify-the-content-of-a-file-using-java – jjlema Feb 09 '15 at 11:41
  • i don`t know how to make it, when i write new object to me xml file how to make it to write it between library tags – alle3x Feb 09 '15 at 11:41
  • @user3613534 but your problem was read the data no? not writing, and write is exactly the same, if you have that objects, book and library, when you serialize using xtream that produce the xml you need. – jjlema Feb 09 '15 at 11:47
  • The code i use for write is this : `FileOutputStream file = new FileOutputStream("a.xml", true); PrintWriter print = new PrintWriter(file); print.write(xstream.toXML(object));` how i make you put every new book object i write withing a library tags in my xml file – alle3x Feb 09 '15 at 11:51
  • @user3613534 when you write xstream.toXML(object), if object is a Library, that xml has already the correct format. – jjlema Feb 09 '15 at 11:54
  • but i added only new books to this library, not a new library and i want every time i run the program and i use method to write new books to put it between library tags in my xml – alle3x Feb 09 '15 at 12:01