diff options
Diffstat (limited to 'docs/samples')
-rw-r--r-- | docs/samples/xpath_error.cpp | 41 | ||||
-rw-r--r-- | docs/samples/xpath_query.cpp | 34 | ||||
-rw-r--r-- | docs/samples/xpath_select.cpp | 25 |
3 files changed, 100 insertions, 0 deletions
diff --git a/docs/samples/xpath_error.cpp b/docs/samples/xpath_error.cpp new file mode 100644 index 0000000..6858f7c --- /dev/null +++ b/docs/samples/xpath_error.cpp @@ -0,0 +1,41 @@ +#include "pugixml.hpp"
+
+#include <iostream>
+
+int main()
+{
+ pugi::xml_document doc;
+ if (!doc.load_file("xgconsole.xml")) return -1;
+
+//[code_xpath_errors
+ // Exception is thrown for incorrect query syntax
+ try
+ {
+ doc.select_nodes("//nodes[#true()]");
+ }
+ catch (const pugi::xpath_exception& e)
+ {
+ std::cout << "Select failed: " << e.what() << std::endl;
+ }
+
+ // Exception is thrown for incorrect query semantics
+ try
+ {
+ doc.select_nodes("(123)/next");
+ }
+ catch (const pugi::xpath_exception& e)
+ {
+ std::cout << "Select failed: " << e.what() << std::endl;
+ }
+
+ // Exception is thrown for query with incorrect return type
+ try
+ {
+ doc.select_nodes("123");
+ }
+ catch (const pugi::xpath_exception& e)
+ {
+ std::cout << "Select failed: " << e.what() << std::endl;
+ }
+//]
+}
diff --git a/docs/samples/xpath_query.cpp b/docs/samples/xpath_query.cpp new file mode 100644 index 0000000..ad62690 --- /dev/null +++ b/docs/samples/xpath_query.cpp @@ -0,0 +1,34 @@ +#include "pugixml.hpp"
+
+#include <iostream>
+#include <string>
+
+int main()
+{
+ pugi::xml_document doc;
+ if (!doc.load_file("xgconsole.xml")) return -1;
+
+//[code_xpath_query
+ // Select nodes via compiled query
+ pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']");
+
+ pugi::xpath_node_set tools = query_remote_tools.evaluate_node_set(doc);
+ std::cout << "Remote tool: ";
+ tools[2].node().print(std::cout);
+
+ // Evaluate numbers via compiled query
+ pugi::xpath_query query_timeouts("sum(//Tool/@Timeout)");
+ std::cout << query_timeouts.evaluate_number(doc) << std::endl;
+
+ // Evaluate strings via compiled query for different context nodes
+ pugi::xpath_query query_name_valid("string-length(substring-before(@Filename, '_')) > 0 and @OutputFileMasks");
+ pugi::xpath_query query_name("concat(substring-before(@Filename, '_'), ' produces ', @OutputFileMasks)");
+
+ for (pugi::xml_node tool = doc.first_element_by_path("Profile/Tools/Tool"); tool; tool = tool.next_sibling())
+ {
+ std::string s = query_name.evaluate_string(tool);
+
+ if (query_name_valid.evaluate_boolean(tool)) std::cout << s << std::endl;
+ }
+//]
+}
diff --git a/docs/samples/xpath_select.cpp b/docs/samples/xpath_select.cpp new file mode 100644 index 0000000..8025e04 --- /dev/null +++ b/docs/samples/xpath_select.cpp @@ -0,0 +1,25 @@ +#include "pugixml.hpp"
+
+#include <iostream>
+
+int main()
+{
+ pugi::xml_document doc;
+ if (!doc.load_file("xgconsole.xml")) return -1;
+
+//[code_xpath_select
+ pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']");
+
+ std::cout << "Tools:";
+
+ for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it)
+ {
+ pugi::xpath_node node = *it;
+ std::cout << " " << node.node().attribute("Filename").value();
+ }
+
+ pugi::xpath_node build_tool = doc.select_single_node("//Tool[contains(Description, 'build system')]");
+
+ std::cout << "\nBuild tool: " << build_tool.node().attribute("Filename").value() << "\n";
+//]
+}
|