aspose file tools*
The moose likes Java in General and the fly likes creating a hashmap of hashmap using xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "creating a hashmap of hashmap using xml" Watch "creating a hashmap of hashmap using xml" New topic
Author

creating a hashmap of hashmap using xml

Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
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

Joined: May 15, 2007
Posts: 18
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

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
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,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: creating a hashmap of hashmap using xml