comparison dep/pugixml/docs/samples/traverse_predicate.cpp @ 367:8d45d892be88 default tip

*: instead of pugixml, use Qt XML features this means we have one extra Qt dependency though...
author Paper <paper@tflc.us>
date Sun, 17 Nov 2024 22:55:47 -0500
parents 886f66775f31
children
comparison
equal deleted inserted replaced
366:886f66775f31 367:8d45d892be88
1 #include "pugixml.hpp"
2
3 #include <string.h>
4 #include <iostream>
5
6 // tag::decl[]
7 bool small_timeout(pugi::xml_node node)
8 {
9 return node.attribute("Timeout").as_int() < 20;
10 }
11
12 struct allow_remote_predicate
13 {
14 bool operator()(pugi::xml_attribute attr) const
15 {
16 return strcmp(attr.name(), "AllowRemote") == 0;
17 }
18
19 bool operator()(pugi::xml_node node) const
20 {
21 return node.attribute("AllowRemote").as_bool();
22 }
23 };
24 // end::decl[]
25
26 int main()
27 {
28 pugi::xml_document doc;
29 if (!doc.load_file("xgconsole.xml")) return -1;
30
31 pugi::xml_node tools = doc.child("Profile").child("Tools");
32
33 // tag::find[]
34 // Find child via predicate (looks for direct children only)
35 std::cout << tools.find_child(allow_remote_predicate()).attribute("Filename").value() << std::endl;
36
37 // Find node via predicate (looks for all descendants in depth-first order)
38 std::cout << doc.find_node(allow_remote_predicate()).attribute("Filename").value() << std::endl;
39
40 // Find attribute via predicate
41 std::cout << tools.last_child().find_attribute(allow_remote_predicate()).value() << std::endl;
42
43 // We can use simple functions instead of function objects
44 std::cout << tools.find_child(small_timeout).attribute("Filename").value() << std::endl;
45 // end::find[]
46 }
47
48 // vim:et