File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Convert xml to HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Convert xml to HashMap" Watch "Convert xml to HashMap" New topic
Author

Convert xml to HashMap

Sam yadav
Greenhorn

Joined: Sep 20, 2012
Posts: 5
Hi,

I want to convert below XML into Hash Map object.


<?xml version="1.0" encoding="iso-8859-1"?/>
<MyPersonalInformation>
<FullName>
<FirstName>Mandrix Mortel</FirstName>
<LastName>Mortini</LastName>
</FullName>
<BirthDate>
<Month>May</Month>
<Date>17</Date>
<Year>1983</Year>
</BirthDate>
<Address>
<Street>St. Michael</Street>
<Province>Palo</Province>
<City>Tacloban</City>
<Country>Philippines</Country>
<PostalCode>6501</PostalCode>
</Address>
</MyPersonalInformation>

Please help.

Thanks,
Sumit
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
HashMap contains Key-Value pairs. Have you figured out in the XML which should be stored as keys and values?

Search for XML parsers to parse the XML and read the necessary data.
Sam yadav
Greenhorn

Joined: Sep 20, 2012
Posts: 5
Actually My requirement is to first convert XML into HashMap and then I have to compare it with other HashMap.
That's why I am looking for the code which will convert XML to HashMap.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I fear you did not get the structure of an HashMap. An HashMap or a Map, stores key-value pairs of Objects like this Map<Key,Value> where Key and Value are Objects.

A refined question is to what you need to compare between the two XML? Is there any specific entity like first name, last name in the given XML?

I see that the XML is about a person. If you need to compare any specific entity then I suggest you to parse both XMLs and store each property in different Person class objects.

Example Person class to hold first name and last name:

You can then compare the Person objects for desired equality.
Sam yadav
Greenhorn

Joined: Sep 20, 2012
Posts: 5
I describe my problem broadly:

I have a hashMap in my project which contains key and value pair.
Now I am getting a XML from another system which may have a same key value pair.
So I need to compare this XML to my HashMap and show the result as how many values are matching or not matching.

That's why i wanted to convert this xml into hashmap and then i can easily compare two hashmap.

So Now tell me how to do that?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sam yadav wrote:I describe my problem broadly:


Yes, we understand that by now. So your next step is to describe it more precisely.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
How far have you gotten? Can you parse the input XML document into a DOM? That would be a good starting point.

Bill
Sam yadav
Greenhorn

Joined: Sep 20, 2012
Posts: 5
Yes i have parsed it using DOM parser and i have a document.
Now next what?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sam yadav wrote:Now next what?


Good question. The answer is, that's up to you. First you have to decide, given some document, what data you want to have in this map. What should the keys be, and what data should correspond to the keys?
Sam yadav
Greenhorn

Joined: Sep 20, 2012
Posts: 5
i have given a xml in my first post.. so i want its tag as key and its values as HashMap value...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. So then you want <MyPersonalInformation> as a key. What value should correspond to it?
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

I think John's suggestion is better.

I would unmarshal the XML to an object (there are libraries to help you do this) and then write a converter which takes the Map and converts it to an object of the same class. At this point, you can then check the two instances for equality.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
I have a hashMap in my project which contains key and value pair.
Now I am getting a XML from another system which may have a same key value pair.
So I need to compare this XML to my HashMap and show the result as how many values are matching or not matching.

Since op has a hashMap in the project as we are told, should it not be reasonable to show what kind of hashMap in terms of HashMap<K,V> so that at a certain moment, one comes to the conclusion: it is the same. So what is the hashMap that one can come to that conclusion comparing the xml as shown?

In fact, associating an xml document with HashMap is never a natural association: that's why all the follow-on questions. The hierachical structure of xml is most naturally associated with a tree with branches and leafs. But it is not even enough. There is a natural ordering (numbering) attached to the tree. But, for that association, it is already a bit less natural... HashMap's ordering is basically an alien concept, in particular, if it is at some point in time need to rebuild itself for the reason of capacity*load-factor saturation. For small xml, probably it can make work fine amid many artificial interpretation one can anticipate of.

If op has some difficulty in producing the identical hashMap he has in mind for comparing, the problem probably is not well thought through.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert xml to HashMap
 
Similar Threads
starting web service
Java in XSLT
scalar functions
problem in using SAX parser
problem understanding xml file