How I get Attribute using by XMLPull parser

前端 未结 2 469
暗喜
暗喜 2020-12-06 16:24

I have an xml file and I show the small part of it, to show the content what I want



        
2条回答
  •  一生所求
    2020-12-06 17:20

    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(
                ""
                        + "Getty Images file"
                        + "2010 Getty Images"
                        + "\"Mitt


    ]]>
    " + "
    ")); while (!"media:content".equals(parser.getName()) && parser.getEventType() != XmlPullParser.START_TAG) { parser.next(); } Log.d("media count -->", parser.getAttributeValue(null, "url")); }

提交回复
热议问题