• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML to Map

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am using JDOM APIs for XML Parsing . I need to parse a XML file & convert it into a Map ( java.util.Map ) or more exactly contents of XML file should be in a Map .

Can any body help me , how it can be done ?
There are some examples I am trying :

1]

<car>
<company>tata</company>
<model>2002</model>
<car>

corresponding Map may be :

car.company = tata
car.model = 2002

( hope it is right )

2]
But the problem is when any element has attributes , how can I convert that type of XML file into Map ?

<customer id="5">
<name>john</name>
<surname>rite</surname>
</customer>

How the Map will be ?

please help ..

thank you very much .
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
But the problem is when any element has attributes , how can I convert that type of XML file into Map ?

<customer id="5">
<name>john</name>
<surname>rite</surname>
</customer>

How the Map will be ?

If you really must convert a hierarchical XML document into a flat key-value structure, this is how I would probably encode attributes:
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any other way also ??

please help me out .

thanks a lot .
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
Is there any other way also ??

Sure. There always is. For example, you can use a pipe character as the path separator... Is there something about my suggested approach that doesn't fit your requirements? It would help if you could actually tell us what is required of this "map".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic