How to get name and value of attributes from xml when using parser libxml2 sax?

I am stuck trying to discover a couple of attribute names and values ​​in some common xmls using libxml2 to parse api in an iPhone application. For my project, parsing speed is really important, so I decided to use libxml2 itself instead of using NSXMLParser.

Now, referring to XMLPerformance, which is a sample of the iPhone SDK for parsing test between NSXMLParser and libxml2, I tried to get the attribute detail in one of the XML parser handlers, as shown 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 not. Please help me if you are a great hacker :)

+3
1

, - :

    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 . , , , , . char, [3] [4] (length = attributes [4] -attributes [3]).

xmlencoding, , / xml, , libxml2 , , typedefs xmlChar unsigned char.

+5

Source: https://habr.com/ru/post/1728699/


All Articles