• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML parsing using JAXB when there are multiple child tags

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a sample XML, would want to create objects and store them in a list. The XML contains the following:


I create the classes student.java


As well as Students.java


And of course the Main program: ParsingXML.java


I want to store the objects in an internal list. I expect it to have 2 entries of id 1 and 2.

However I get the following output


Only the second entry is shown here. I believe it is getting overwritten. Could anyone provide me some insights to have both the values, please


 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch!

Your Students class contains only one Student. This needs to be a List<Student>. This should map each student element to an element in the list.

This will probably already work (I'm a bit too lazy to try it now), but then the name of the Java property is odd; for a list property, you expect the property name to indicate it's a multiple. I think you can use @XmlElement to fix that:
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch again. I am adding your thread to our XML forum.
reply
    Bookmark Topic Watch Topic
  • New Topic