Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating a hashmap of hashmap using xml

 
Mohit Sinha
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:
----------------------------------------------------
<refType="Customer">
<values>
<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>
</values>
</refType>
<refType="Occupation">
<values>
<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>
</values>
</refType>

-----------------------------------------------------
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.


Regards,
 
Oliver Kamps
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Cheers,
Oliver
 
Stan James
(instanceof Sidekick)
Ranch Hand
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
OUTERMOST HASHMAP
-----------------
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.


Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic