Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
685 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  Probleme xpath(libxml2) : un seul parsing par xmlDoc ?

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Probleme xpath(libxml2) : un seul parsing par xmlDoc ?

n°1789379
deff_bacar​di
Posté le 19-09-2008 à 15:07:59  profilanswer
 

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 :
  1. #include <string.h>
  2. #include <sstream>
  3. #include <libxml/parser.h>
  4. #include <libxml/xpath.h>
  5. #include <iostream>
  6. using namespace std;
  7. void xpathParsing(xmlXPathContext xpathCtx){
  8. string Xpth("//" );
  9. Xpth.append("*[contains(name(),'toto')]" ); //the // allow to select all  descendants of the root node
  10. xmlXPathObjectPtr xpathObject;
  11. xmlNodeSetPtr n;
  12. xmlNodePtr propsNode;
  13.     xpathObject = xmlXPathEvalExpression((const xmlChar*)Xpth.data(), xpathCtx);
  14.     if(xpathObject == NULL) {
  15.  string err("Error: unable to evaluate xpath expression \n" );
  16.  throw (err);
  17.     }
  18. if(!xmlXPathNodeSetIsEmpty(xpathObject->nodesetval)){
  19.  n=xpathObject->nodesetval;
  20.  for (int i=0; i < n->nodeNr; i++) {
  21.   cout << n->nodeTab[i]->name << endl;
  22.   if(n->nodeTab[i]->properties == NULL){
  23.    cout << "NO PROPS" << endl;
  24.   }else{
  25.    for (propsNode = n->nodeTab[i]; propsNode->properties != NULL; propsNode->properties = n->nodeTab[i]->properties->next) {
  26.     cout << "+" <<propsNode->properties->name << endl;
  27.    }
  28.   }
  29.   if(n->nodeTab[i]->xmlChildrenNode == NULL){
  30.    cout << "NO CHILD" << endl;
  31.   }else{
  32.    while (n->nodeTab[i]->xmlChildrenNode != NULL) {
  33.     if((n->nodeTab[i]->xmlChildrenNode->type == XML_ELEMENT_NODE)){
  34.      cout << "-" << n->nodeTab[i]->xmlChildrenNode->name << endl;
  35.     }
  36.     n->nodeTab[i]->xmlChildrenNode = n->nodeTab[i]->xmlChildrenNode->next;
  37.    }
  38.   }
  39.  }
  40. }else{
  41.  cout << "empty" << endl;
  42. }
  43. xmlXPathFreeObject(xpathObject);
  44. }
  45. int main(int argc, char **argv) {
  46. xmlDocPtr doc;
  47. xmlXPathContext xpathCtx;
  48. xmlInitParser();
  49. xmlXPathInit();
  50. xmlKeepBlanksDefault(0);
  51. doc = xmlReadFile("TestFESA210.xml",NULL, 0);
  52. if (doc == NULL) {
  53.  string err("Invalid XML document\n" );
  54.  throw (err);
  55. }
  56. xpathCtx = xmlXPathNewContext(doc);
  57. if(xpathCtx == NULL) {
  58.  string err("Error: unable to create new XPath context\n" );
  59.  throw (err);
  60. }
  61. cout << "PREMIER XPATH" << endl;
  62.     xpathParsing(xpathCtx);
  63. cout << endl << "SECOND XPATH" << endl;
  64. xpathParsing(xpathCtx);
  65. xmlXPathFreeContext(xpathCtx);
  66. xmlFreeDoc(doc);
  67. xmlCleanupParser();
  68. xmlMemoryDump();
  69. return (1);
  70. }


 
merci d'avance

mood
Publicité
Posté le 19-09-2008 à 15:07:59  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  Probleme xpath(libxml2) : un seul parsing par xmlDoc ?

 

Sujets relatifs
Probleme encodage de caractères étendus après copier/collerproblème création de forum
Problème javascript / DOMRequete SQL avec GROUP BY probleme
Probléme méthode paintComponent()problème sur les sprites
Problème avec PHP photo moduleProblème require_once
problème requête updateproblème de contrainte Check à deux tables:
Plus de sujets relatifs à : Probleme xpath(libxml2) : un seul parsing par xmlDoc ?


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR