• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert xml to HashMap

 
Sam yadav
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i have parsed it using DOM parser and i have a document.
Now next what?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have given a xml in my first post.. so i want its tag as key and its values as HashMap value...
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So then you want <MyPersonalInformation> as a key. What value should correspond to it?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic