iphone - 使用libxml2 sax解析器时如何从xml中获取属性的名称和值?

标签 iphone c objective-c xml libxml2

我一直在尝试通过使用 libxml2 解析 iPhone 应用程序上的 api 来检测某些通用 xml 中属性的名称和值对。对于我的项目,解析速度非常重要,所以我决定使用 libxml2 本身而不是使用 NSXMLParser。

现在,引用 XMLPerformance,它是 iPhone SDK 的一个示例,用于 NSXMLParser 和 libxml2 之间的解析基准,我试图在一个 XML 解析器处理程序中获取属性的详细信息,如下所示,但我不知 Prop 体如何来检测它。

/* for example, <element key="value" /> */
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 = @""; /* expected: key */
            NSString* val = @""; /* expected: value */
            [attributeDict setValue:val forKey:key];
        }
     }
}

我看到了the libxml2 document ,但我不能。如果你是伟大的黑客,请帮助我 :)

最佳答案

通过查看链接的文档,我认为这样的事情可能会起作用:

    for (int i=0; i<nb_attributes; i++) 
    { 
        // if( *attributes[4] != '\0' ) // something needed here to null terminate the value
        NSString* key = [NSString stringWithCString: attributes[0] encoding: xmlencoding];
        NSString* val = [NSString stringWithCString: attributes[3] encoding: xmlencoding];
        [attributeDict setValue:val forKey:key];
        attributes += 5;
    } 

这假设每个属性总是有 5 个字符串指针。由于没有另外说明,我认为可以安全地假设值字符串是空终止的,并且给出结束指针只是为了方便计算长度。如果结束指针不指向空字符,您只需要将属性 [3] 到属性 [4] 的字符解释为值字符串(长度 = 属性 [4]-属性 [3])。

xmlencoding 可能需要是 xml 文档/实体的编码,除了 libxml2 已经进行了一些转换,尽管这似乎不太可能,因为它将 xmlChar 类型定义为 unsigned char。

关于iphone - 使用libxml2 sax解析器时如何从xml中获取属性的名称和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2075894/

相关文章:

ios - 适用于通用应用程序的iOS设计方向问题

iphone - iOS:ImageIO 在矩形图像上调整大小

c - K&R 练习 1-18 在遇到 EOF 时给出段错误

ios - 从动画 View 中获取 transform.rotation.z 值

iphone - NSPredicate 来查明实体的实例是否有父代?

iphone - 在 Xcode 4.2 中以编程方式编写 iPhone 应用程序的 UITableView

c - 制作简单的linux内核模块

c - 我如何在互联网上找到 pbmpak.c 文件?

ios - 抑制不必要的零

ios - 如何删除具有合理文本对齐的多行 UILabel 中的孤儿和寡妇?