2012-05-29 18 views
7

Esta es una estructura xml que quiero analizar utilizando el análisis libxml.cómo analizar los datos xml usando el análisis libxml

enter image description here

enter image description here

¿Cómo puedo obtener el valor del atributo para la etiqueta "campaña" es decir ID y para la etiqueta de "imagen" es decir url y tamaño.

Si utilizo estos valores, puedo extraer los valores de la etiqueta "código" y la etiqueta "nombre".

static const char *kName_campaign = "campaign"; 
static const NSUInteger kLength_campaign = 9; 
static const char *kName_code = "code"; 
static const NSUInteger kLength_code = 5; 
static const char *kName_name = "name"; 
static const NSUInteger kLength_name = 5; 

Luego obtengo el código y el nombre de la campaña actual y la próxima todos juntos. Este es el código que uso en el delegado al que se llama cuando se realiza el análisis.

static void endElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI) 
{  
    LibXMLParser *parser = (LibXMLParser *)ctx; 

    if (parser.parsingASong == NO) return; 

    if (prefix == NULL) 
    { 
     if (!strncmp((const char *)localname, kName_campaign, kLength_campaign)) 
     { 
      [parser finishedCurrentSong]; 
      parser.parsingASong = NO; 
     } 
     else if (!strncmp((const char *)localname, kName_code, kLength_code)) 
     { 
      parser.currentSong.title = [parser currentString]; 
      NSLog(@"Code :: %@",[parser currentString]); 
     } 
     else if (!strncmp((const char *)localname, kName_name, kLength_name)) 
     { 
      parser.currentSong.category = [parser currentString]; 
      NSLog(@"Name :: %@",[parser currentString]); 
     } 
    } 
} 

¿Cómo puedo obtener los valores de los atributos de principio a final de Identificación de la etiqueta de "campaña". url y tamaño desde la etiqueta "imagen"?

+0

Realy gracias por que Mat. – NiKKi

+0

Fuera de interés, ¿por qué no está utilizando 'NSXMLParser'? –

+0

Cuz es más rápido y amigable con la memoria ... – NiKKi

Respuesta

6
static void startElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, 
int nb_defaulted, const xmlChar **attributes) 
{ 

if (nb_attributes > 0) 
{ 
    NSMutableDictionary* attributeDict = [NSMutableDictionary dictionaryWithCapacity:(NSUInteger)[NSNumber numberWithInt:nb_attributes]]; 
    for (int i=0; i<nb_attributes; i++) 
    { 
     NSString* key = [NSString stringWithCString:(const char*)attributes[0] encoding:NSUTF8StringEncoding]; 
     NSString* val = [[NSString alloc] initWithBytes:(const void*)attributes[3] length:(attributes[4] - attributes[3]) encoding:NSUTF8StringEncoding]; 
     attributes += 5; 

     [attributeDict setValue:val forKey:key]; 
    } 
} 
} 

Prueba esto ..

Cuestiones relacionadas