Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <libxml++/libxml++.h>
- 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 Attribute node (dynamic_cast on reference can throw [fail fast])
- Attribute &attribute = dynamic_cast<Attribute&>(*firstNodeInResult);
- // Get value of the attribute
- ustring attributeValue = attribute.get_value();
- // Print attribute value
- cout << attributeValue << endl;
- }
- <!-- file.xml -->
- <root>
- <a>
- <b attr="I want to get this"> </b>
- </a>
- </root>
- I want to get this
- c++ `pkg-config libxml++-2.6 --cflags` `pkg-config libxml++-2.6 --libs` file.cpp
- <users noofids="1">
- <user user="vin" password="abc"/>
- </users>
- #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;
- }
- #include <iostream>
- #include <libxml++/libxml++.h>
- 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;
- }
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <root>
- <ApplicationSettings>
- <level_three>hello world</level_three>
- </ApplicationSettings>
- </root>
- g++ test.cpp -o test `pkg-config libxml++-2.6 --cflags` `pkg-config libxml++-2.6 --libs`
- ./test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement