0

如何可以解析在Android这个XML文件,以获得大的图像XML解析在Android的

<image size="small">http://userserve-ak.last.fm/serve/34/62210477.png</image> <image size="medium">http://userserve-ak.last.fm/serve/64/62210477.png</image> 
<image size="large">http://userserve-ak.last.fm/serve/126/62210477.png</image> 

的网址是什么访问图像大小的URL最好的方法=“大”上较新的Android版本的特定属性像ICS和果冻豆 * 编辑的问题* 作为答案中提供的答案我根据我的xml文件做了一些改变现在ii获取数据,但我想访问大图像的URL我需要做什么来获得大图像url

代码修改(我认为)

@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 
    elementOn = true; 
    if (localName.equals("artist")) 
    { 
     data = new XMLGettersSetters(); 
    } else if (localName.equals("image")) { 
     /** 
     * We can get the values of attributes for eg. if the CD tag had an attribute(<CD attr= "band">Akon</CD>) 
     * we can get the value "band". Below is an example of how to achieve this. 
     * 
     * String attributeValue = attributes.getValue("attr"); 
     * data.setAttribute(attributeValue); 
     * 
     * */ 
    } 
} 
/** 
* This will be called when the tags of the XML end. 
**/ 
@Override 
public void endElement(String uri, String localName, String qName) 
     throws SAXException { 
    elementOn = false; 
    /** 
    * Sets the values after retrieving the values from the XML tags 
    * */ 
    if (localName.equalsIgnoreCase("name")) 
     data.setTitle(elementValue); 
    else if (localName.equalsIgnoreCase("listeners")) 
     data.setArtist(elementValue); 
    else if (localName.equalsIgnoreCase("url")) 
     data.setCountry(elementValue); 
    else if (localName.equalsIgnoreCase("image")) 
     data.setCompany(elementValue); 
    } 
/** 
* This is called to get the tags value 
**/ 
@Override 
public void characters(char[] ch, int start, int length) 
     throws SAXException { 
    if (elementOn) { 
     elementValue = new String(ch, start, length); 
     elementOn = false; 
    } 
} 

请帮我 谢谢

回答

0

你可以使用SAX或DOM解析器为此,我建议SAX解析器,因为它不太内存占用和快速。

对于例如更详细的检查这link

@Override 
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 

     //do some checking. 

     if(atts.getValue("size").equals("large")&&localName.equals("Image")) 
     { 
      //do some action 
     }; 

    } 
+0

我去与你的链接,下载并提出根据我的链接做一些修改,但现在我得到的小图像的URL。我需要更改哪些代码才能获取大图片网址?请检查主要问题,我只是现在编辑 –

+0

让我知道它是否工作 – DroidBot

+0

老兄我卡住了,我无奈你可以帮助我,你可以写简单的代码,从这个链接检索大图像 http:// ws。 audioscrobbler.com/2.0/?method=track.getInfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&track=believe –

0
public static String getTextValue(Element ele, String tagName) { 
    String textVal = ""; 
    NodeList nl = ele.getElementsByTagName(tagName); 
    if(nl != null && nl.getLength() > 0) { 
     Element el = (Element)nl.item(0); 
     if(el.getFirstChild()!=null)  
     { 
      textVal = el.getFirstChild().getNodeValue(); 
     } 
    } 
    return textVal; 
} 

or try using ele.getAttribute(tagName) 

上述方法输入元素节点和变量名“形象” ...

+0

我遵循本教程现在我从XML获取数据,但我如何访问大型图像的URL ..如何访问特定属性..请检查主要问题中的代码我只是编辑它 –

0

从解析图像的URL将xml的url转换为一个字符串并将其用于任何目的。

你可以从jsoup.org看看jsoup。我在几个项目中使用它,并且对它感到满意。

1

试试这个:

XMLParser parser = new XMLParser(); 
    String URL = "http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=akon&api_key=your_api_key"; 
    String xml = parser.getXmlFromUrl(URL); // getting XML 
    Document doc = parser.getDomElement(xml); // getting DOM element 

    NodeList nl = doc.getElementsByTagName("album"); 

    for (int i = 0; i < nl.getLength(); i++) { 
     Element e = (Element) nl.item(i); 
     Log.e("name", parser.getValue(e, "name")); 
     NodeList k = e.getElementsByTagName("image"); 
     for (int j = 0; j < k.getLength(); j++) { 
      Element e1 = (Element) k.item(j); 
      if(e1.getAttribute("size").equals("large")) 
      Log.e("ImageURL", parser.getValue(e1, "image")); 
     } 
    }