Search code examples
androidxmlxml-parsingandroid-xmldomparser

Parse XML data from url using DOM Parser


I want to parse xml file from url :

http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=cher&api_key=5d6ce941674603e4bb75cfad6cfa13b7

I want to parse following tags of the file :

<artist>
<name>Cher</name>
<image size="medium">http://userserve-ak.last.fm/serve/64/62286415.png</image>
</artist>

But i don't know how to get the value of these two tags only.

I have tried the example code from http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/

But it does not showing to parse same tag having different attribute value. Can anyone guide me how this is done?

Thanx in advance.


Solution

  • Thankx. I solved my problem from this url :

    Getting element using attribute

    if(str.equals("image"))
             {
                 n = item.getElementsByTagName(str);
    
                 for (int i = 0; i < n.getLength(); i++) {
                        Node subNode = n.item(i);
    
                        if (subNode.hasAttributes()) {
                            NamedNodeMap nnm = subNode.getAttributes();
    
                            for (int j = 0; j < nnm.getLength(); j++) {
                                Node attrNode = nnm.item(j);
    
                                if (attrNode.getNodeType() == Node.ATTRIBUTE_NODE) {
                                    Attr attribute = (Attr) attrNode;
    
                                   if( attribute.getValue().equals("medium"))
                                   {
                                       return this.getElementValue(n.item(i));
                                   }
                                }
                            }               
                        }
                    }
    
             }