Obsah

XML

http://stackoverflow.com/questions/9387610/what-xml-parser-should-i-use-in-c

libXML2

Přímé procházení

<configuration>
<timestamp>2016-04-01 10:00</timestamp>
 
<methods>
..
</methods>
 
<syssettings>
..
</syssettings>
 
</configuration>
void parseCfg(xmlDocPtr doc, xmlNodePtr cur) {
 
    xmlChar *key;
 
    cur = cur->xmlChildrenNode;
    while (cur != NULL) {
 
        if (cur->type == XML_ELEMENT_NODE) {
 
            if ((!xmlStrcmp(cur->name, (const xmlChar *)"timestamp"))) {
                key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
                xmlFree(key);
            }
 
            if ((!xmlStrcmp(cur->name, (const xmlChar *)"methods"))) {
                parseMet(doc, cur->xmlChildrenNode);
            }
 
            if ((!xmlStrcmp(cur->name, (const xmlChar *)"syssettings"))) {
                parsePref(doc, cur->xmlChildrenNode);
            }
 
        }
        cur = cur->next;
    }
    return;
}
 
int parseXmlBuf(const char *buf, int size) {
 
    xmlDocPtr doc;
    xmlNodePtr cur;
 
    doc = xmlParseMemory(buf,size);
 
    if (doc == NULL ) {
        return 1;
    }
 
    cur = xmlDocGetRootElement(doc);
 
    if (cur == NULL) {
        xmlFreeDoc(doc);
        return 2;
    }
 
    // check if the buf is XML configuration (root equal to configuration}
    if (xmlStrcmp(cur->name, (const xmlChar *) "configuration")) {
        xmlFreeDoc(doc);
        return 3;
    }
 
    parseCfg(doc, cur);
 
    xmlFreeDoc(doc);
    return 0;
}
..
<parameter name="PerspectiveSeason" value="183"/>
<parameter name="LockConfig" value="0"/>
<parameter name="extServices" value="1"/>
<parameter name="currentMaxThreads" value="2"/>
<parameter name="resolveFilter" value="LAN"/>
..
   // look for parameters
    while (cur != NULL) {
        if (cur->type == XML_ELEMENT_NODE) {
 
            if ((!xmlStrcmp(cur->name, (const xmlChar *)"parameter"))) {
 
                // look for name and value of parameter
                char *name = NULL;
                char *value = NULL;
                xmlAttr *attribute = NULL;
 
                attribute = cur->properties;
                while(attribute && attribute->name && attribute->children) {
 
                    if ((!xmlStrcmp(attribute->name, (const xmlChar *)"name"))) {
                        if (name) xmlFree(name);
                        name = (char*)xmlNodeListGetString(cur->doc, attribute->children, 1);
                    }
                    if ((!xmlStrcmp(attribute->name, (const xmlChar *)"value"))) {
                        if (value) xmlFree(value);
                        value = (char*)xmlNodeListGetString(cur->doc, attribute->children, 1);
                    }
 
                    attribute = attribute->next;
                }
 
                if ((value)&&(name)) {
                    if (!strcmp((char*)name,"extServices")) {
                        ext_allowed = (!strcmp((char*)value,"1"));
                    }
                }
 
                if (name) xmlFree(name);
                if (value) xmlFree(value);
            }
        }
        cur = cur->next;
    }

Vyhledávání přes Xpath