如何使用libxml解析来解析xml数据 [英] how to parse the xml data using libxml parsing

查看:169
本文介绍了如何使用libxml解析来解析xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想用libxml解析解析的xml结构。

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

如何获取广告系列标记的属性值,即 ID 和image标记即 url 大小

How can i get the attribute value for "campaign" tag i.e ID and for the "image" tag i.e url and size.

如果我使用这些值,我可以提取code标签的值和name标签。

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

我如何获得属性广告系列标记中 id 的从头到尾的值。来自image标签的网址尺寸

How can i get the attributes values from start to bottom of id from "campaign" tag. url and size from "image" tag?

推荐答案

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

试试这个..

这篇关于如何使用libxml解析来解析xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆