• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

What collection type should be used for this datastructure

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

can you please share your recommendation:
I am getting an XML in the below format from upstream system. I would like to convert into a JAVA POJO object. Since it is a key value pair,
1. Is it ok to use HashMap?
2. Is HashMap<String, Object> ok for this? since the value can again hold another name value pair? Is it a good practive to "Object" generic data type in Hashmap?

 
Saloon Keeper
Posts: 9564
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like it would be best implemented as a basic class.
 
Naresh Shanmugam
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carey Brown.

Some times the upstream can send additional name value pair, which we dont know. since we dont know all the possible keys of student class, can we use any other datastructure like HashMap<String, Object> ? The value here is Object type because emailList, phoneList is of object type but studId and Name are string type. Is there any other datastructure we can use since we dont know all the possible keys?
 
Carey Brown
Saloon Keeper
Posts: 9564
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could certainly use HashMap<String,Object> but depending on how complex you XML is it could get pretty messy.

Leave it as XML and use XPATH to access members.

Use a hybrid approach where you use standard java fields for known data and a HashMap<String,Object> for any unknown data.

You may want a wrapper class with a HashMap field that can provide assorted supporting methods.
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A HashMap (or any Map for that matter) wont help with repeated keys e.g. "emailID" is repeated in your structure above. Using a Object in generics type has a very limited usecase and I dont think that's applicable here.
Java already allows you to take an XML and convert it to a DOM : https://docs.oracle.com/javase/tutorial/jaxp/dom/readingXML.html

Considering all the data is String, you can come up with your own structure like this :
I'm also curious, if you are not aware of the keys/structure, what is your intention after making the Java POJO ?
 
Always! Wait. Never. Shut up. Look at this tiny ad.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic