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
158 views
in Technique[技术] by (71.8m points)

android - how to parse the value in the attribute in xml parsing

Hi i have a webservice like this

<audio title="Terry Waychuk Pure2010" audio="http://www.boodang.com/api/audio/**Terry_Waychuk**/Terry_Waychuk_Pure2010.mp3" description="Terry Waychuk Pure2010" createddate = "23-01-2011" thumbnail="http://www.boodang.com/api/thumb/boodean_logo.png" />

<audio title="Terry Waychuk frequency2010" audio="http://www.boodang.com/api/audio/**Terry_Waychuk**/Terry_Waychuk_frequency2010.mp3" description="Terry Waychuk frequency2010" createddate = "23-01-2011" thumbnail="http://www.boodang.com/api/thumb/boodean_logo.png" />



<audio title="The Grimey Tech RandomHero FlooRLayer Scooty OH Mazik Boodang 10 Year Anniversary Promo Mix" audio="http://www.boodang.com/api/audio/**The_Grimey_Tech**/The_Grimey_Tech_RandomHero_FlooRLayer_Scooty_OH_Mazik_Boodang_10_Year_Anniversary_Promo_Mix.mp3" description="The Grimey Tech RandomHero FlooRLayer Scooty OH Mazik Boodang 10 Year Anniversary Promo Mix" createddate = "23-01-2011" thumbnail="http://www.boodang.com/api/thumb/boodean_logo.png" />

<audio title="The Grimey Tech and Titus 1 Warper Warfare" audio="http://www.boodang.com/api/audio/**The_Grimey_Tech**/The_Grimey_Tech_and_Titus_1_Warper_Warfare.mp3" description="The Grimey Tech and Titus 1 Warper Warfare" createddate = "23-01-2011" thumbnail="http://www.boodang.com/api/thumb/boodean_logo.png" />
                                  .
                                  .
                                  .
                                  .
                                  .

the first attributes have a mp3 file in same file name . the file name denoted in ** ** and next two also have a mp3 file in same file name.so i want to show a listview according to file names.first i want show a listview like this

 Terry_Waychuk

 The_Grimey_Tech

after click on anyone of these then show a listview as mp3 file titles(that first two in Terry_Waychuk and next two in The_Grimey_Tech). so please tell me how to get the particular name in attribute value and also how to add that particular mp3 files to that folder(Terry_Waychuk or The_Grimey_Tech).

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

One way using the android XmlPullParser (you didn't specify which one you were using) is to pull the attributes into a Map<String, String> when you receive a XmlPullParser.START_TAG, so, assuming a main parse::

private void parseContent(XmlPullParser parser) 
    throws XmlPullParserException,IOException,Exception {
    int eventType;
    while((eventType=parser.next()) != XmlPullParser.END_TAG) {
        if (eventType == XmlPullParser.START_TAG) {
            Log.d(MY_DEBUG_TAG,"Parsing Attributes for ["+parser.getName()+"]");
            Map<String,String> attributes = getAttributes(parser);
        }
        else if(eventType==...);
        else {
            throw new Exception("Invalid tag at content parse");
        }
    }
}

private Map<String,String>  getAttributes(XmlPullParser parser) throws Exception {
    Map<String,String> attrs=null;
    int acount=parser.getAttributeCount();
    if(acount != -1) {
        Log.d(MY_DEBUG_TAG,"Attributes for ["+parser.getName()+"]");
        attrs = new HashMap<String,String>(acount);
        for(int x=0;x<acount;x++) {
            Log.d(MY_DEBUG_TAG,"["+parser.getAttributeName(x)+"]=" +
                    "["+parser.getAttributeValue(x)+"]");
            attrs.put(parser.getAttributeName(x), parser.getAttributeValue(x));
        }
    }
    else {
        throw new Exception("Required entity attributes missing");
    }
    return attrs;
}

The parser.getName() returns the name of the entity associated to the XmlPullParser.START_TAG.

Hope this helps


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

1.4m articles

1.4m replys

5 comments

56.9k users

...