How to retrieve node and attribute values from xml file in C++ using libxml2 by using xpath ?
Thanks in advance, Bhargava
sp1.xml:
<users noofids="1">
<user user="vin" password="abc"/>
</users>
Program:
#include <libxml/xpath.h>
#include <libxml/tree.h>
#include <iostream>
using namespace std;
int
main (int argc, char **argv)
{
char ID[25];
xmlInitParser ();
//LIBXML_TEST_VERSION
xmlDoc *doc = xmlParseFile ("sp1.xml");
xmlXPathContext *xpathCtx = xmlXPathNewContext (doc);
xmlXPathObject *xpathObj =
xmlXPathEvalExpression ((xmlChar *) "users/user", xpathCtx);
xmlNode *node = xpathObj->nodesetval->nodeTab[0];
xmlAttr *attr = node->properties;
while (attr)
{
//if(!xmlStrcmp(attr->name,(const xmlChar *)"noofids"))
//sprintf(ID,"%s",attr->children->content);
std::cout << "Attribute name: " << attr->name << " value: " << attr->
children->content << std::endl;
attr = attr->next;
}
//std::cout<<"ID: "<<ID<<endl;
return 0;
}
i got output by trying by my own