Comment récupérer des valeurs de noeud et d’atsortingbut à partir d’un fichier xml en C ++ en utilisant libxml2 en utilisant xpath?
Merci d’avance, Bhargava
Comme il est marqué C ++, je suppose que vous pouvez utiliser les librairies libxml ++.
J’ai écrit un programme simple qui:
DomParser
find()
sur le noeud racine du document pour accéder à l’atsortingbut. Atsortingbute
get_value()
Voici le code:
#include #include using namespace std; using namespace Glib; using namespace xmlpp; int main(int argc, char* argv[]) { // Parse the file DomParser parser; parser.parse_file("file.xml"); Node* rootNode = parser.get_document()->get_root_node(); // Xpath query NodeSet result = rootNode->find("/root/a/b/@attr"); // Get first node from result Node *firstNodeInResult = result.at(0); // Cast to Atsortingbute node (dynamic_cast on reference can throw [fail fast]) Atsortingbute &atsortingbute = dynamic_cast(*firstNodeInResult); // Get value of the atsortingbute ussortingng atsortingbuteValue = atsortingbute.get_value(); // Print atsortingbute value cout << attributeValue << endl; }
Compte tenu de cette entrée:
Le code va sortir:
I want to get this
Pour comstackr cela sur un système Unix:
c++ `pkg-config libxml++-2.6 --cflags` `pkg-config libxml++-2.6 --libs` file.cpp
sp1.xml:
Programme:
#include #include #include 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: "<
j'ai obtenu en essayant par mes propres
Voici un exemple de la façon d’obtenir la valeur de xpath que vous essayez d’évaluer avec libxml ++.
Sur la base de la réponse d’Alexandre Jasmin mais son seul exemple montre comment imprimer un atsortingbut xpath et il n’est pas sortingvial de savoir comment imprimer la valeur du Node
car il faut le convertir en un object spécifique (sa réponse génère également une exception).
#include #include using namespace std; using namespace Glib; using namespace xmlpp; int main(int argc, char* argv[]) { // Parse the file DomParser parser; parser.parse_file("sample.xml"); Node* root = parser.get_document()->get_root_node(); // Xpath query NodeSet result = root->find("/root/ApplicationSettings/level_three"); // Get first element from result Element *first_element = (Element *)result.at(0); // Print the content of the Element cout << first_element->get_child_text()->get_content() << endl; }
sample.xml
hello world
comstackr
g++ test.cpp -o test `pkg-config libxml++-2.6 --cflags` `pkg-config libxml++-2.6 --libs`
courir
./test