Pergunta

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

<svg>
    <g>
       <path d="11"/>
    </g>
</svg>

I want to see a value of "d" on my c++ program, when I come to this point :

   xmlNode *cur_node = NULL;

    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);
    }    
}

I dont know what I need to do, please help me.

Foi útil?

Solução

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);

Outras dicas

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);
}    

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;
        }
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top