我有一个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选项卡.我该怎么做
我做了以下代码
if(parser.getName().equalsIgnoreCase("media:content")) { Log.d("media count-->",parser.getAttributeCount()+""); }
所以这给我-1.
嘿,如果有人给我提示如何我可以得到图像网址.
解决方法
致电
getAttributeValue如下
parser.getAttributeValue(null,"url")
在你的if语句里面.确保getEventType()等于START_TAG,因为当您的解析器设置为媒体的END_TAG部分时,您当前的if语句也将为true:内容(将给出-1属性计数).
编辑
既然你有这么多麻烦,希望这个小测试功能能够做到你想要的:
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,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")); }