comparison dep/pugixml/docs/samples/xpath_query.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 <iostream>
4 #include <string>
5
6 int main()
7 {
8 pugi::xml_document doc;
9 if (!doc.load_file("xgconsole.xml")) return -1;
10
11 // tag::code[]
12 // Select nodes via compiled query
13 pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']");
14
15 pugi::xpath_node_set tools = query_remote_tools.evaluate_node_set(doc);
16 std::cout << "Remote tool: ";
17 tools[2].node().print(std::cout);
18
19 // Evaluate numbers via compiled query
20 pugi::xpath_query query_timeouts("sum(//Tool/@Timeout)");
21 std::cout << query_timeouts.evaluate_number(doc) << std::endl;
22
23 // Evaluate strings via compiled query for different context nodes
24 pugi::xpath_query query_name_valid("string-length(substring-before(@Filename, '_')) > 0 and @OutputFileMasks");
25 pugi::xpath_query query_name("concat(substring-before(@Filename, '_'), ' produces ', @OutputFileMasks)");
26
27 for (pugi::xml_node tool = doc.first_element_by_path("Profile/Tools/Tool"); tool; tool = tool.next_sibling())
28 {
29 std::string s = query_name.evaluate_string(tool);
30
31 if (query_name_valid.evaluate_boolean(tool)) std::cout << s << std::endl;
32 }
33 // end::code[]
34 }
35
36 // vim:et