deff_bacardi | Bonjour,
voici mon soucis :
j'utilise libxml2 et xpath en c++.
Dans ma phase d'init, je crée le xmlDocPtr et le xmlXPathContextPtr, puis j'appelle 2 fois la fonction qui évalue le xpath avec le xmlXPathContextPtr en parametre.
et bien le second résultat est tout le temps erroné !
En effet, la liste des éléments trouvés sont vides d'attributs et de children.
voici le code :
Code :
- #include <string.h>
- #include <sstream>
- #include <libxml/parser.h>
- #include <libxml/xpath.h>
- #include <iostream>
- using namespace std;
- void xpathParsing(xmlXPathContext xpathCtx){
- string Xpth("//" );
- Xpth.append("*[contains(name(),'toto')]" ); //the // allow to select all descendants of the root node
- xmlXPathObjectPtr xpathObject;
- xmlNodeSetPtr n;
- xmlNodePtr propsNode;
- xpathObject = xmlXPathEvalExpression((const xmlChar*)Xpth.data(), xpathCtx);
- if(xpathObject == NULL) {
- string err("Error: unable to evaluate xpath expression \n" );
- throw (err);
- }
- if(!xmlXPathNodeSetIsEmpty(xpathObject->nodesetval)){
- n=xpathObject->nodesetval;
- for (int i=0; i < n->nodeNr; i++) {
- cout << n->nodeTab[i]->name << endl;
- if(n->nodeTab[i]->properties == NULL){
- cout << "NO PROPS" << endl;
- }else{
- for (propsNode = n->nodeTab[i]; propsNode->properties != NULL; propsNode->properties = n->nodeTab[i]->properties->next) {
- cout << "+" <<propsNode->properties->name << endl;
- }
- }
- if(n->nodeTab[i]->xmlChildrenNode == NULL){
- cout << "NO CHILD" << endl;
- }else{
- while (n->nodeTab[i]->xmlChildrenNode != NULL) {
- if((n->nodeTab[i]->xmlChildrenNode->type == XML_ELEMENT_NODE)){
- cout << "-" << n->nodeTab[i]->xmlChildrenNode->name << endl;
- }
- n->nodeTab[i]->xmlChildrenNode = n->nodeTab[i]->xmlChildrenNode->next;
- }
- }
- }
- }else{
- cout << "empty" << endl;
- }
- xmlXPathFreeObject(xpathObject);
- }
- int main(int argc, char **argv) {
- xmlDocPtr doc;
- xmlXPathContext xpathCtx;
- xmlInitParser();
- xmlXPathInit();
- xmlKeepBlanksDefault(0);
- doc = xmlReadFile("TestFESA210.xml",NULL, 0);
- if (doc == NULL) {
- string err("Invalid XML document\n" );
- throw (err);
- }
- xpathCtx = xmlXPathNewContext(doc);
- if(xpathCtx == NULL) {
- string err("Error: unable to create new XPath context\n" );
- throw (err);
- }
- cout << "PREMIER XPATH" << endl;
- xpathParsing(xpathCtx);
- cout << endl << "SECOND XPATH" << endl;
- xpathParsing(xpathCtx);
- xmlXPathFreeContext(xpathCtx);
- xmlFreeDoc(doc);
- xmlCleanupParser();
- xmlMemoryDump();
- return (1);
- }
|
merci d'avance |