Uživatelské nástroje

Nástroje pro tento web


xml

Toto je starší verze dokumentu!


XML

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

libXML2

  • jednoduchý příklad:
<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;
}
xml.1460535445.txt.gz · Poslední úprava: (upraveno mimo DokuWiki)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki