• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML to Map

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any other way also ??

please help me out .

thanks a lot .
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic