如何在XML解析中解析属性值

5
你好,我有一个像这样的Web服务:

<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" />
                                  .
                                  .
                                  .
                                  .
                                  .

第一个属性有一个同名的mp3文件。文件名用 ** ** 表示,接下来的两个属性也有同名的mp3文件。因此,我想按照文件名显示一个列表视图。首先,我想要像这样显示一个列表视图:

 Terry_Waychuk

 The_Grimey_Tech

点击其中任何一个后,将显示一个列表视图,其中包含mp3文件标题(在Terry_Waychuk中的前两个和在The_Grimey_Tech中的下两个)。请告诉我如何获取属性值中的特定名称,并如何将特定的mp3文件添加到该文件夹(Terry_Waychuk或The_Grimey_Tech)。

1个回答

18

使用Android XmlPullParser(您没有指定使用哪个版本)的一种方法是在收到XmlPullParser.START_TAG时将属性提取到Map<String,String>中,因此假设一个主要解析器:

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,"\t["+parser.getAttributeName(x)+"]=" +
                    "["+parser.getAttributeValue(x)+"]");
            attrs.put(parser.getAttributeName(x), parser.getAttributeValue(x));
        }
    }
    else {
        throw new Exception("Required entity attributes missing");
    }
    return attrs;
}
parser.getName() 返回与 XmlPullParser.START_TAG 相关联的实体名称。希望这可以帮到你。

<Category value ="A"> <Sub_Category value ="C"> </Sub_Category> </Category> 如何使用XMLPullparser获取A的值 - Gnanam R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接