Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

android - How I get Attribute using by XMLPull parser

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

I do the following code

if(parser.getName().equalsIgnoreCase("media:content"))
{
    Log.d("media count-->",parser.getAttributeCount()+"");
}       

So this gives me -1.

Hey if anyone give me hint for how i can get the image url.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Call getAttributeValue like the following

parser.getAttributeValue(null, "url") 

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).

EDIT Since 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"));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...