且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-07 11:32:34

通过查看链接的文档,我认为类似的方法可能有效:

    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个字符串指针.除非另有说明,否则我认为可以安全地假定值字符串以null终止,并且仅给出结束指针以允许进行容易的长度计算.如果结束指针未指向空字符,则您只需将从attribute [3]到attribute [4]的char解释为值字符串(长度= attribute [4] -attributes [3]).>

xmlencoding可能需要作为xml文档/实体的编码,除了libxml2已经进行了一些转换,尽管这似乎不太可能,因为它将typedefs xmlChar转换为未签名的char.

I got stuck on trying to detect the pair of name and value of the attributes in some general xmls by using libxml2 for parsing the api on iPhone application. For my project, the parsing speed is really important, so I decided to use libxml2 itself instead of using NSXMLParser.

Now, as referring to XMLPerformance that is a sample of iPhone SDK for the parsing benchmark between NSXMLParser and libxml2, I tried to get the detail of attribute in one of XML parser handler as below, but I don't know exactly how to detect it.

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

I saw the libxml2 document, but I can't. Please help me if you are great hacker :)

From looking at the linked documentation I'd think something like this might work:

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

This assumes that there are always 5 string pointers for each attribute. As it is not otherwise noted I think it is safe to assume that the value string is null terminated and the end pointer is only given to allow easy length calculation. In case the end pointer does not point to a null char you would need to interpret only the chars from attributes[3] up to attributes[4] as value string (length = attributes[4]-attributes[3]).

xmlencoding probably needs to be the encoding of the xml document/entity except libxml2 does some conversion already although this seems unlikely as it typedefs xmlChar to unsigned char.