iphone - 如何使用libxml解析解析xml数据

标签 iphone ios

这是我要使用 libxml 解析来解析的 xml 结构。

enter image description here

enter image description here

如何获取“campaign”标签的属性值,即 ID 和“image”标签的属性值,即 urlsize

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

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;

然后我得到当前和即将进行的事件的代码和名称。 这是我在解析完成时调用的委托(delegate)中使用的代码。

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

如何从“campaign”标签中获取 id 从头到尾的属性值。 urlsize 来自“图片”标签?

最佳答案

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

试试这个..

关于iphone - 如何使用libxml解析解析xml数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10799423/

相关文章:

iphone - 滚动以加载 UIView 中的其他内容

iphone - 设备上的 iPhone kccparamerror 中的 AES 加密

iphone - 单元格 View 公开按钮及相关方法

iphone - 指定单元格 imageView 的宽度

ios - Swift 中的 NSURL 问题

android - 在 AIR Mobile 应用程序中显示列表项(一千个以上)的最佳方式是什么?

iphone - 如何在 iPhone 中捕获应用程序不活动时间

objective-c - 弹出当前 UIViewController 并推送另一个

ios - 在 MapKit Swift 中禁用 Mylocation Marker Tap

ios - 在 Cordova 6.4.0 中构建 ios 平台时出错