File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed in parsing XML to a HashMap

 
Gopi Chand Maddula
Ranch Hand
Posts: 40
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20164
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20164
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic