Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse the xml data using libxml parsing

Tags:

ios

iphone

This is a xml structure that I want to parse using libxml parsing.

enter image description here

enter image description here

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?

like image 505
NiKKi Avatar asked May 29 '12 12:05

NiKKi


People also ask

Which method is used to parse an XML document?

DOM API, which parses XML documents and builds a tree representation of the documents in memory. Use a DOMParser object to parse with DOM. SAX API, which processes an XML document as a stream of events, which means that a program cannot access random locations in a document.

How can parsing the XML data using DOM and SAX?

The two common ways to parse an XML document are given below: DOM Parser: Parsing the document by loading all the content of the document and creating its hierarchical tree structure. SAX Parser: Parsing based on event-based triggers. It does not require the complete loading of content.

Can regex parse XML?

XML is a context-free language, while regular expressions are regular, so regular expressions can—provably—never fully parse XML. That said, regexes can be useful as tools in the parsing process (e.g. to find the next occurrence of a non-quote-character).


1 Answers

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

like image 149
DivineDesert Avatar answered Oct 03 '22 19:10

DivineDesert