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?
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.
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.
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).
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With