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

后端 未结 3 919
甜味超标
甜味超标 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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 08:33

    I'm not sure I understand the question, but it sounds like you want to print the attribute "d" in the element "path". In the code above, you need something like this:

    xmlChar *d = xmlGetProp(cur_node, "d");
    ... do something ...
    xmlFree(d);
    
    0 讨论(0)
  • something like that ?

    static void
    print_element_names(xmlNode * a_node)
    
    {
    
       xmlNode *cur_node = NULL;
       xmlChar        *d;
    
       for (cur_node = a_node; cur_node; cur_node = cur_node->next) {    
    
          if(xmlStrEqual(xmlCharStrdup("path"),cur_node->name)){ 
              printf("element: %s\n", cur_node->name);
          }
    
          print_element_names(cur_node->children);  
    
          if(xmlGetProp(cur_node, "d")){  
    
          printf("wspolrzedne: %s\n", d);
       }
    
       xmlFree(d);
    }    
    
    0 讨论(0)
提交回复
热议问题