how to read a part of xml file in C++ using Libxml2

后端 未结 3 929
甜味超标
甜味超标 2020-12-12 07:46

Hello i need to know \"how to read a part of xml file in C++ using Libxml2\". In my xml file I have :


    
       

        
3条回答
  •  余生分开走
    2020-12-12 08:24

    Below function will read complete xml with node and values,

    xmlDocPtr pFilePointer = xmlParseFile(xmlFile);
    xmlNodePtr pNodePointer = xmlDocGetRootElement(pFilePointer);
    
    void readXML(const xmlDocPtr cpFilePointer, const xmlNodePtr cpNodePointer) {
        string value;
        xmlDocPtr pFilePointer = cpFilePointer;
        xmlNodePtr pNodePointer = cpNodePointer;
        while (pNodePointer != NULL) {
            if (NULL != pNodePointer->xmlChildrenNode) {
                xmlNodePtr pParentPointer = pNodePointer;
                string node = (const char *)pParentPointer->name;
                pNodePointer = pNodePointer->xmlChildrenNode;
                if (!xmlStrcmp(pNodePointer->name, (const xmlChar *)"text")) {
                    xmlNodeListGetStringWrapper(pFilePointer, pNodePointer, value);
                    cout << node << ":" << value << endl;
                } else {
                    LOG2((TEXT("no need to read node %s\n"), pParentPointer->name));
                }
            } else if (NULL != pNodePointer->next) {
                pNodePointer = pNodePointer->next;
            } else {
                pNodePointer = pNodePointer->parent;
                pNodePointer = pNodePointer->next;
            }
        }
    }
    

提交回复
热议问题