I am using following code to convert a XML file data into HashMap. I had modified the old code. Now it is running properly. but only for XML file having following structure:
< root>
< tag>
< subtag>< /subtag>
< subtag>< /subtag>
:
</tag>
< tag>
< subtag>< /subtag>
< subtag>< /subtag>
:
< /tag>
< /root>
If i change the structure like, i introduce new sub-subtags, or i use empty tags. Then it is not working properly.
public ArrayList getTagList(Document document) {
ArrayList Alltags = new ArrayList();
HashMap tag = new HashMap();
HashMap leaftag = new HashMap();
getTagList( document.getRootElement(),Alltags,tag ,leaftag );
return Alltags;
}
public void getTagList(Element element,ArrayList Alltags, HashMap tag,HashMap leaftag ) {
for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
Node node = element.node(i);
if ( node instanceof Element ) {
getTagList( (Element) node,Alltags, tag,leaftag );
if(((Element) node).isTextOnly()){
leaftag.put(node.getName(), node.getText());
}
else{
tag.put(node.getName(), leaftag);
Alltags.add(tag);
tag= new HashMap();
leaftag= new HashMap();
}
}//End If
}//End For
}
Plese help & suggest what extra changes i can do? So all sort of XMLs can be converted into HashMap
If you suggest xStream. Please tell me how can i parse a XML file using xStream directly.