我貼出xml文件的一部分代碼,顯示我想獲得的內容
<media:content medium="image" url="http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/111010-romney-health-4p.thumb.jpg">
<media:credit role="provider">Getty Images file</media:credit>
<media:copyright>2010 Getty Images</media:copyright>
<media:text><![CDATA[<p><a href="http://www.msnbc.msn.com/id/44854320/ns/politics-decision_2012/"><img align="left" border="0" src="http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/111010-romney-health-4p.thumb.jpg" alt="Mitt Romney speaks at the National Press Club March 5, 2010 in Washington, D.C." style="margin:0 5px 5px 0" /></a></p><br clear="all" />]]></media:text>
</media:content>
現在我想檢索 URL tab,如何檢索呢?
用的下面的代碼:
if(parser.getName().equalsIgnoreCase("media:content"))
{
Log.d("media count-->",parser.getAttributeCount()+"");
}
然後給出的是-1.
請求大家給點意見,如何得到圖像的url?
在if語句中調用 getAttributeValue
parser.getAttributeValue(null, "url")
當 media:content 的解析器設置為END_TAG 部分,現在的if 語句也可能是 true,所以確保 getEventType()和 START_TAG 相同。
public void parseXml() throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader(
"<media:content medium=\"image\" url=\"http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/111010-romney-health-4p.thumb.jpg\">"
+ "<media:credit role=\"provider\">Getty Images file</media:credit>"
+ "<media:copyright>2010 Getty Images</media:copyright>"
+ "<media:text><![CDATA[<p><a href=\"http://www.msnbc.msn.com/id/44854320/ns/politics-decision_2012/\"><img align=\"left\" border=\"0\" src=\"http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/111010-romney-health-4p.thumb.jpg\" alt=\"Mitt Romney speaks at the National Press Club March 5, 2010 in Washington, D.C.\" style=\"margin:0 5px 5px 0\" /></a></p><br clear=\"all\" />]]></media:text>"
+ "</media:content>"));
while (!"media:content".equals(parser.getName()) && parser.getEventType() != XmlPullParser.START_TAG) {
parser.next();
}
Log.d("media count -->", parser.getAttributeValue(null, "url"));
}