2011-10-11 15 views
11

Tengo un archivo XML y muestro la pequeña parte de ella, para mostrar el contenido de lo que quieroCómo llego atributo mediante por XMLPull analizador

<media:content medium="image" url="http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/111010-romney-health-4p.thumb.jpg"> 
       <media:credit role="provider">Getty Images file</media:credit> 
       <media:copyright>2010 Getty Images</media:copyright> 
       <media:text><![CDATA[<p><a href="http://www.msnbc.msn.com/id/44854320/ns/politics-decision_2012/"><img align="left" border="0" src="http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/111010-romney-health-4p.thumb.jpg" alt="Mitt Romney speaks at the National Press Club March 5, 2010 in Washington, D.C." style="margin:0 5px 5px 0" /></a></p><br clear="all" />]]></media:text> 
      </media:content> 

Ahora quiero recuperar la pestaña URL. ¿Cómo hago esto

hago el siguiente código

if(parser.getName().equalsIgnoreCase("media:content")) 
{ 
    Log.d("media count-->",parser.getAttributeCount()+""); 
}  

Así que esto me da -1.

Hola, si alguien me da pistas sobre cómo puedo obtener la URL de la imagen.

Respuesta

22

llamada getAttributeValue como la siguiente

parser.getAttributeValue(null, "url") 

interior de su sentencia if. Asegúrese de que getEventType() sea igual a START_TAG ya que su instrucción if actual también se evaluará como verdadera cuando su analizador esté configurado en la parte END_TAG de su contenido multimedia (lo que le daría un recuento de atributo -1).

EDITAR Puesto que usted está teniendo tantos problemas, espero que esta función de prueba pequeño hace lo que quiere:

public void parseXml() throws XmlPullParserException, IOException 
{ 
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
    XmlPullParser parser = factory.newPullParser(); 
    parser.setInput(new StringReader(
      "<media:content medium=\"image\" url=\"http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/111010-romney-health-4p.thumb.jpg\">" 
        + "<media:credit role=\"provider\">Getty Images file</media:credit>" 
        + "<media:copyright>2010 Getty Images</media:copyright>" 
        + "<media:text><![CDATA[<p><a href=\"http://www.msnbc.msn.com/id/44854320/ns/politics-decision_2012/\"><img align=\"left\" border=\"0\" src=\"http://msnbcmedia.msn.com/j/MSNBC/Components/Photo/_new/111010-romney-health-4p.thumb.jpg\" alt=\"Mitt Romney speaks at the National Press Club March 5, 2010 in Washington, D.C.\" style=\"margin:0 5px 5px 0\" /></a></p><br clear=\"all\" />]]></media:text>" 
        + "</media:content>")); 

    while (!"media:content".equals(parser.getName()) && parser.getEventType() != XmlPullParser.START_TAG) { 
     parser.next(); 
    } 
    Log.d("media count -->", parser.getAttributeValue(null, "url")); 
} 
+0

da error nulo puntero excepción –

+0

tiene cualquier otro método para hacerlo –

+0

Ese es el método correcto para hacerlo. ¿Cambió su declaración if para también verificar el tipo de evento del analizador? Además, ¿estás seguro de que estás leyendo el XML correcto? –

2
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException { 
    parser.require(XmlPullParser.START_TAG, ns, "enclosure"); 
    final String link = parser.getAttributeValue(null, "url"); 
    return link; 
} 

Esto funciona para mí en androide con XmlPullParser.

Cuestiones relacionadas