Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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


 
Marshal
Posts: 22389
121
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: 73748
332
  • 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.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic