how to parse the xml data using libxml parsing

ios, iphone

Solution

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

Try this..

Problem

This is a xml structure that I want to parse using libxml parsing. How can i get the attribute value for "campaign" tag i.e ID and for the "image" tag i.e url and size. If I use these values, i can extract the values of "code" tag and "name " tag. ``` 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; ``` Then I get the code and name of current and upcoming campaign all together. This is the code I use in the delegate which gets called when parsing is done. ``` 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]); } } } ``` How can i get the attributes values from start to bottom of id from "campaign" tag. url and size from "image" tag?

Original source