From 813cabb5e67bab646a838890fa55363ad566ee11 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Wed, 7 Jul 2010 14:47:48 +0000 Subject: docs: Added traverse samples, minor manual fixes, 'getting' section is almost finished git-svn-id: http://pugixml.googlecode.com/svn/trunk@567 99668b35-9821-0410-8761-19e4c4f06640 --- docs/samples/traverse_iter.cpp | 25 ++++++++++++++++++++ docs/samples/traverse_predicate.cpp | 46 +++++++++++++++++++++++++++++++++++++ docs/samples/traverse_walker.cpp | 33 ++++++++++++++++++++++++++ docs/samples/tree.xml | 2 +- 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 docs/samples/traverse_iter.cpp create mode 100644 docs/samples/traverse_predicate.cpp create mode 100644 docs/samples/traverse_walker.cpp (limited to 'docs/samples') diff --git a/docs/samples/traverse_iter.cpp b/docs/samples/traverse_iter.cpp new file mode 100644 index 0000000..b134f2f --- /dev/null +++ b/docs/samples/traverse_iter.cpp @@ -0,0 +1,25 @@ +#include "pugixml.hpp" + +#include + +int main() +{ + pugi::xml_document doc; + if (!doc.load_file("xgconsole.xml")) return -1; + + pugi::xml_node tools = doc.child("Profile").child("Tools"); + + //[code_traverse_iter + for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it) + { + std::cout << "Tool:"; + + for (pugi::xml_attribute_iterator ait = it->attributes_begin(); ait != it->attributes_end(); ++ait) + { + std::cout << " " << ait->name() << "=" << ait->value(); + } + + std::cout << std::endl; + } + //] +} diff --git a/docs/samples/traverse_predicate.cpp b/docs/samples/traverse_predicate.cpp new file mode 100644 index 0000000..19fa32f --- /dev/null +++ b/docs/samples/traverse_predicate.cpp @@ -0,0 +1,46 @@ +#include "pugixml.hpp" + +#include +#include + +//[code_traverse_predicate_decl +bool small_timeout(pugi::xml_node node) +{ + return node.attribute("Timeout").as_int() < 20; +} + +struct allow_remote_predicate +{ + bool operator()(pugi::xml_attribute attr) const + { + return strcmp(attr.name(), "AllowRemote") == 0; + } + + bool operator()(pugi::xml_node node) const + { + return node.attribute("AllowRemote").as_bool(); + } +}; +//] + +int main() +{ + pugi::xml_document doc; + if (!doc.load_file("xgconsole.xml")) return -1; + + pugi::xml_node tools = doc.child("Profile").child("Tools"); + + //[code_traverse_predicate_find + // Find child via predicate (looks for direct children only) + std::cout << tools.find_child(allow_remote_predicate()).attribute("Filename").value() << std::endl; + + // Find node via predicate (looks for all descendants in depth-first order) + std::cout << doc.find_node(allow_remote_predicate()).attribute("Filename").value() << std::endl; + + // Find attribute via predicate + std::cout << tools.last_child().find_attribute(allow_remote_predicate()).value() << std::endl; + + // We can use simple functions instead of function objects + std::cout << tools.find_child(small_timeout).attribute("Filename").value() << std::endl; + //] +} diff --git a/docs/samples/traverse_walker.cpp b/docs/samples/traverse_walker.cpp new file mode 100644 index 0000000..f712eee --- /dev/null +++ b/docs/samples/traverse_walker.cpp @@ -0,0 +1,33 @@ +#include "pugixml.hpp" + +#include + +const char* node_types[] = +{ + "null", "document", "element", "pcdata", "cdata", "comment", "pi", "declaration" +}; + +//[code_traverse_walker_impl +struct simple_walker: pugi::xml_tree_walker +{ + virtual bool for_each(pugi::xml_node& node) + { + for (int i = 0; i < depth(); ++i) std::cout << " "; // indentation + + std::cout << node_types[node.type()] << ": name='" << node.name() << "', value='" << node.value() << "'\n"; + + return true; // continue traversal + } +}; +//] + +int main() +{ + pugi::xml_document doc; + if (!doc.load_file("tree.xml")) return -1; + + //[code_traverse_walker_traverse + simple_walker walker; + doc.traverse(walker); + //] +} diff --git a/docs/samples/tree.xml b/docs/samples/tree.xml index dbe3301..81b6f4f 100644 --- a/docs/samples/tree.xml +++ b/docs/samples/tree.xml @@ -2,7 +2,7 @@ some text - + some more text -- cgit v1.2.3