2011-01-28 90 views
5

嗨,我有一個Web服務這樣的如何解析的屬性值在XML解析

<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文件name.so我想根據文件names.first我想表明這樣

Terry_Waychuk 

The_Grimey_Tech 

後一個ListView顯示列表視圖點擊其中的任何一個,然後顯示一個列表視圖作爲mp3文件標題(前兩個在Terry_Waychuk,後兩個在The_Grimey_Tech)。所以請告訴我如何獲取屬性值中的特定名稱以及如何將特定的mp3文件添加到該文件夾​​(Terry_Waychuk或The_Grimey_Tech)。使用

回答

18

的一種方式了Android XmlPullParser(沒有指定您所使用的一個)是屬性拉成Map<String, String>當您收到XmlPullParser.START_TAG,所以,假設主解析::

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關聯的實體的名稱。

希望這有助於

+0

<類別值= 「A」> \t \t \t \t \t \t \t如何獲得A,使用的值XMLPullparser – 2013-02-28 14:05:17