comparison dep/pugixml/docs/samples/xpath_query.cpp @ 55:d10b6c6b432e

add xml lib, we will need to use it eventually
author Paper <mrpapersonic@gmail.com>
date Tue, 26 Sep 2023 12:37:08 -0400
parents
children
comparison
equal deleted inserted replaced
54:466ac9870df9 55:d10b6c6b432e
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