Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

creating a hashmap of hashmap using xml

Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Currently I have a xml document. I have to convert this xml file into a java HashMap.

Here is how the xml looks:
<value name="refcode">11</value>
<value lang="en" name="refval"> Staff </value>
<value lang="fr" name="refval"> Employi </value>

<value name="refcode">21</value>
<value lang="en" name="refval"> Corporate </value>
<value lang="fr" name="refval"> Grande Enterprise </value>
<value name="refcode">12</value>
<value lang="en" name="refval"> Architect </value>
<value lang="fr" name="refval"> Architectes </value>

<value name="refcode">22</value>
<value lang="en" name="refval"> Actor </value>
<value lang="fr" name="refval"> Acteurs </value>

I will have numerous such refType as mentioned above each ranging with a few or hundreds of values within it.
Now I want to create a Hashmap from this xml in such a way that the data to the layer above is accessible in a easier way.
So the Hashmap would look something like this

Outer hashmap
key="Customer" value=<<Inner hashmap>>
Inner Hashmap(en) Inner Hashmap(fr)
--------------------- -----------------
key="11" value="Staff" key="11" value="Employi"
key="21" value="Corporate" key="21" value="Grande Ent"

key="Occupation" value=<<Inner hashmap>>
Inner Hashmap(en) Inner Hashmap(fr)
--------------------- -----------------
key="12" value="Architect" key="12" value="Architectes"
key="22" value="Actor" key="22" value="Acteurs"

The usage pattern would be something like this. The code trying to access the hashmap would pass the outer hasmap key say Customer or Occupation and locale i.e. 'en' or 'fr' as mentioned in the above sample & it returns the inner hasmap accordingly.
I would want to know your suggestions/ideas how do i go about structuring this hashmap scenario. Also do post your thoughts on how do I go about creating this hashmap from xml.

Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if you have control over your files you might want to look at Java ResourceBundle and the corresponding properties files--resource bundles are intended to handle internationalization issues similar to what you describe.

JAXB is pretty convenient for parsing XML; it allows you to access your XML data through generated Java classes--constructing the HashMap from these classes is pretty straightforward.

(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see the maps working nicely enough. To get Customer type 11 in English:

I named each map after the key. You might have better ideas on that.

You'll have to process the XML slightly out of order, which is just a bit tricky to keep straight.

Does that make sense?
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,

Thanks for your reply.
Currently I am processing the XML (using xml beans) in a straight order as the xml i.e. refType -- values. So now I have a Hashmap which looks something like this
Reftypename Hashmap
Refcode Hashmap
Language Values

Though this approach is easier in creation of Hashmap I have to do some dynamic processing to render a hashmap for a particular reftypename, refcode, language combination.
I guess the approach you suggested will not have that extra processing to do at runtime but as you mentioned in your post, processing the xml out of order would be a bit tricky. Thats the part where I would need some help.

Can some one throw some pointers on how to go about out of the order processing of the xml.

Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic