2011-01-28 16 views
5

Ciao ho un webservice come questocome analizzare il valore dell'attributo in XML parsing

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

i primi attributi hanno un file mp3 in stesso nome di file. il nome del file indicato nel ** ** e accanto due hanno anche un file mp3 in stesso file name.so voglio mostrarvi un controllo ListView in base al file di names.first voglio mostrare un ListView come questo

Terry_Waychuk 

The_Grimey_Tech 

dopo clicca su qualcuno di questi quindi mostra una lista come titoli di file mp3 (i primi due in Terry_Waychuk e i successivi due in The_Grimey_Tech). quindi per favore dimmi come ottenere il nome particolare nel valore dell'attributo e anche come aggiungere quel particolare file mp3 a quella cartella (Terry_Waychuk o The_Grimey_Tech).

risposta

18

Un modo utilizzando l'androide XmlPullParser (non è stato specificato quale si sta utilizzando) è quello di tirare gli attributi in una Map<String, String> quando si riceve un XmlPullParser.START_TAG, quindi, assumendo una parse principale ::

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

Il parser.getName() restituisce il nome dell'entità associata allo XmlPullParser.START_TAG.

Spero che questo aiuti

+0

\t \t \t \t \t \t \t come ottenere il valore di A con XMLPullparser –