This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Help needed in parsing XML to a 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 "Help needed in parsing XML to a HashMap" Watch "Help needed in parsing XML to a HashMap" New topic
Author

Help needed in parsing XML to a HashMap

Gopi Chand Maddula
Ranch Hand

Joined: May 11, 2010
Posts: 40

Hi JavaRanch,

I am facing some difficulty in parsing the XML into a HashMap.
My XML Structure is as follows.



and my code is as follows



As my code is working fine for the posted XML but i had a new xml where there are another two subnodes where i hace inserted in subnode2



if i am using this xml i am not able to read the data from subNodeA and subNodeB....

So do i need to modify some thing to my code to make it work.
i want to make that code as generic as possible which would read any XML (i.e., even if i add another sub Node to SubNodeA)and give the HashMap as output


Please Help....!
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
i want to make that code as generic as possible which would read any XML


Not happening son
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Sounds like you would need to build your own modification to existing technology - how about:

Adding attributes to a custom schema to indicate the repeating elements, key and value elements.
-or-
Defining XPath statements to locate the repeating elements, then extract the key and value data items. These statements could reside in an instruction/configuration file for your generalized reader class.

Bill

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Gopi Chand Maddula wrote:I want to make that code as generic as possible which would read any XML and give the HashMap as output


Then you are going to have to make your requirements a lot clearer than what they are so far. For many XML documents there isn't an obvious mapping to a set of key-value pairs.
Gopi Chand Maddula
Ranch Hand

Joined: May 11, 2010
Posts: 40

Please let me know am i doing correct process to parse the XML or do we have any predefined format for parsing the XML.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, you are parsing the XML correctly. You do produce a DOM, don't you? So then that takes care of the parsing.

What you do with the DOM afterwards depends on your requirements. Which you haven't said what they are. You said you want to create a HashMap, but you haven't said what the rules are for deciding what to put into that HashMap.

Gopi Chand Maddula wrote:do we have any predefined format for parsing the XML.


I don't know what that means. "Format for parsing the XML"? All I can say is that you have to decide what you want to do before you start typing out code at your keyboard. Why don't you start with that? Explain, in English, what are the rules for converting that XML example into a HashMap.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed in parsing XML to a HashMap
 
Similar Threads
NULL values in FK field
xml parsing
need to parse a xml which doesn't have a parent node.
xml parsing with dom
XSLT - is it possible?