0

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.

2 Answers 2

2

from the link you provided, I have just extract a small part :

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName("artist");
    // looping through all item nodes <artist>
    for (int i = 0; i < nl.getLength(); i++) {
       Element e = (Element) nl.item(i);
       String name = parser.getValue(e, "name"));
       String image = parser.getValue(e, "image")); 

       //if you want the artist 'Cher' sigh ;)
       if (name.equals("Cher")){
          //do whatever you want
       }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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));
                               }
                            }
                        }               
                    }
                }

         }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.