wood burning stoves*
The moose likes XML and Related Technologies and the fly likes XML to Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML to Map" Watch "XML to Map" New topic
Author

XML to Map

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Is there any other way also ??

please help me out .

thanks a lot .
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML to Map
 
Similar Threads
PPR with two tr:selectOneChoice problem
problems with bean:write
Response xml in Apache CXF using JAX-RS
Newbie validation with textfile
mixed output