问题描述
我有一个XML文件,我展示了它的小部分,以显示我想要的内容
I have an xml file and I show the small part of it, to show the content what I want
<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>
现在我想要检索的网址选项卡。如何做到这一点
Now I want to retrieve the URL tab. How I do this
我做了以下code
if(parser.getName().equalsIgnoreCase("media:content"))
{
Log.d("media count-->",parser.getAttributeCount()+"");
}
所以,这给了我-1。
So this gives me -1.
嘿,如果有人给我暗示如何我可以得到图像的URL。
Hey if anyone give me hint for how i can get the image url.
推荐答案
呼叫getAttributeValue像下面的
parser.getAttributeValue(null, "url")
你的if语句内。确保 getEventType()
等于START_TAG因为你当前的if语句也计算为true,当你的解析器设置为媒体的END_TAG部分:内容(这将使你-1的属性计数)。
inside of your if statement. Make sure getEventType()
is equal to START_TAG since your current if statement will also evaluate to true when your parser is set to the END_TAG portion of your media:content (which would give you a -1 attribute count).
修改既然你有这么多的麻烦,我希望这个小测试功能,你想要做什么:
EDITSince you are having so much trouble, I hope this little test function does what you want:
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"));
}
这篇关于我如何获得属性利用XMLPull分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!