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
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.
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.
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.