Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <libxml++/libxml++.h>
  3.  
  4. using namespace std;
  5. using namespace Glib;
  6. using namespace xmlpp;
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10. // Parse the file
  11. DomParser parser;
  12. parser.parse_file("file.xml");
  13. Node* rootNode = parser.get_document()->get_root_node();
  14.  
  15. // Xpath query
  16. NodeSet result = rootNode->find("/root/a/b/@attr");
  17.  
  18. // Get first node from result
  19. Node *firstNodeInResult = result.at(0);
  20. // Cast to Attribute node (dynamic_cast on reference can throw [fail fast])
  21. Attribute &attribute = dynamic_cast<Attribute&>(*firstNodeInResult);
  22.  
  23. // Get value of the attribute
  24. ustring attributeValue = attribute.get_value();
  25.  
  26. // Print attribute value
  27. cout << attributeValue << endl;
  28. }
  29.  
  30. <!-- file.xml -->
  31. <root>
  32. <a>
  33. <b attr="I want to get this"> </b>
  34. </a>
  35. </root>
  36.  
  37. I want to get this
  38.  
  39. c++ `pkg-config libxml++-2.6 --cflags` `pkg-config libxml++-2.6 --libs` file.cpp
  40.  
  41. <users noofids="1">
  42. <user user="vin" password="abc"/>
  43. </users>
  44.  
  45. #include <libxml/xpath.h>
  46. #include <libxml/tree.h>
  47. #include <iostream>
  48. using namespace std;
  49.  
  50. int
  51. main (int argc, char **argv)
  52. {
  53. char ID[25];
  54. xmlInitParser ();
  55. //LIBXML_TEST_VERSION
  56. xmlDoc *doc = xmlParseFile ("sp1.xml");
  57. xmlXPathContext *xpathCtx = xmlXPathNewContext (doc);
  58. xmlXPathObject *xpathObj =
  59. xmlXPathEvalExpression ((xmlChar *) "users/user", xpathCtx);
  60. xmlNode *node = xpathObj->nodesetval->nodeTab[0];
  61. xmlAttr *attr = node->properties;
  62. while (attr)
  63. {
  64. //if(!xmlStrcmp(attr->name,(const xmlChar *)"noofids"))
  65. //sprintf(ID,"%s",attr->children->content);
  66. std::cout << "Attribute name: " << attr->name << " value: " << attr->
  67. children->content << std::endl;
  68. attr = attr->next;
  69. }
  70. //std::cout<<"ID: "<<ID<<endl;
  71. return 0;
  72. }
  73.  
  74. #include <iostream>
  75. #include <libxml++/libxml++.h>
  76.  
  77. using namespace std;
  78. using namespace Glib;
  79. using namespace xmlpp;
  80.  
  81. int main(int argc, char* argv[])
  82. {
  83. // Parse the file
  84. DomParser parser;
  85. parser.parse_file("sample.xml");
  86. Node* root = parser.get_document()->get_root_node();
  87.  
  88. // Xpath query
  89. NodeSet result = root->find("/root/ApplicationSettings/level_three");
  90.  
  91. // Get first element from result
  92. Element *first_element = (Element *)result.at(0);
  93.  
  94. // Print the content of the Element
  95. cout << first_element->get_child_text()->get_content() << endl;
  96. }
  97.  
  98. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  99. <root>
  100. <ApplicationSettings>
  101. <level_three>hello world</level_three>
  102. </ApplicationSettings>
  103. </root>
  104.  
  105. g++ test.cpp -o test `pkg-config libxml++-2.6 --cflags` `pkg-config libxml++-2.6 --libs`
  106.  
  107. ./test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement