【问题标题】:how to parse the value in the attribute in xml parsingxml解析中如何解析属性中的值
【发布时间】:2011-01-28 10:29:09
【问题描述】:

你好,我有这样的网络服务

<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)。

【问题讨论】:

    标签: android xml parsing listview


    【解决方案1】:

    使用 android XmlPullParser 的一种方法(您没有指定您使用的是哪一个)是在收到 XmlPullParser.START_TAG 时将属性拉入Map&lt;String, String&gt;,因此,假设一个主要解析::

    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 关联的实体的名称。

    希望对你有帮助

    【讨论】:

    • 如何使用 XMLPullparser 获取 A 的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    相关资源
    最近更新 更多