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

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

问题描述

我有一个这样的Web服务

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文件。文件名表示在**和未来两年也有name.so我想根据names.first我想表明这样的列表视图文件显示列表视图在同一个文件中的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 (你没有指定哪一个你正在使用)是拉属性到地图&LT;字符串,字符串&GT; 当您收到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,"\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 的实体的名称。

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

希望这有助于

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

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