tags:

views:

66

answers:

4

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.

A: 

In your loop, you are assigning a new hashmap to internalTags variable. So the next addings will fall into this new hashmap (not the original one). Please use another variable name like subInternalTags or something! :)

helios
Will u tell me how? i need to assign a new hashmap to internalTags. Otherwise i'll get only last element in last. i can not use any number of hashmap. because XML file can have any format.
articlestack
I was trying to suggest code, but I've realized I don't really know what kind of map you want. If you could write an example of the map you want to obtain it'd be great. I need it because I don't know if you want a recursive map (map of name->value, where value is the text in case of leaves or another map in case of complex tags); or you want another kind of map. Anyway it's still weird the code in the `else` becase it adds the variable passed as parameter to a map and then you initialize it... you are losing all the subsequent things collected in the new tag/leftag maps.
helios
A: 

Not sure how to explain... but you put tags in internalTags on one level but only copy them in roottags on the next level, thus you 'loose' the internalTags for the last level...

Anyway your algo is difficult to understand, what are the different maps and list meant for...

Try to explain what you try to achieve...

pgras
First of all, as per my understaning your statement should come under comment not answer. Different maps and list are being used because after every recursion i lost old memory blocks. I need to preserve them until at least one complete tag(including subtags) is processed and put to a hashMap.
articlestack
Should have written an answer and a comment... I explained why you lost the last tag. But I also wanted to suggest a simpler algo and thus wanted to really understand the purpose of your code...
pgras
A: 

Do you want a map of nested maps or are you just looking for a generic key value structure for representing any XML document? If it is the latter check out example 1 from my blog post on Service Data Objects (SDO).

Blaise Doughan
A: 

I had done it using google multimap. thanks for all of your support.

Although your suggestions are welcome

articlestack