xml解析中如何解析属性中的值 [英] how to parse the value in the attribute in xml parsing

查看:35
本文介绍了xml解析中如何解析属性中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的网络服务

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

第一个属性具有相同文件名的 mp3 文件.** ** 中表示的文件名和接下来的两个文件名也有一个 mp3 文件.所以我想根据文件名显示一个列表视图.首先我想显示一个这样的列表视图

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

单击其中任何一个后,将列表视图显示为 mp3 文件标题(Terry_Waychuk 中的前两个和 The_Grimey_Tech 中的后两个).所以请告诉我如何在属性值中获取特定名称以及如何将该特定 mp3 文件添加到该文件夹​​(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).

推荐答案

使用 android XmlPullParser 的一种方法(您没有指定使用的是哪个)是将属性拉入 Map<String, String> 当您收到 XmlPullParser.START_TAG 时,假设主解析::

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;
}

parser.getName() 返回与 XmlPullParser.START_TAG 关联的实体的名称.

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

希望能帮到你

这篇关于xml解析中如何解析属性中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆