From 9b57b1bdd72e43475992c427b5e8f285ad007c54 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine@gmail.com" Date: Sun, 29 Apr 2012 17:58:05 +0000 Subject: docs: Added range-based for documentation and sample git-svn-id: http://pugixml.googlecode.com/svn/trunk@904 99668b35-9821-0410-8761-19e4c4f06640 --- docs/manual.qbk | 23 +++++++++++++++++++++++ docs/samples/traverse_rangefor.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 docs/samples/traverse_rangefor.cpp diff --git a/docs/manual.qbk b/docs/manual.qbk index 9fff574..5059631 100644 --- a/docs/manual.qbk +++ b/docs/manual.qbk @@ -928,6 +928,24 @@ This is an example of using these functions ([@samples/traverse_base.cpp]): [endsect] [/contents] +[section:rangefor Range-based for-loop support] + +[#xml_node::children][#xml_node::attributes] +If your C++ compiler supports range-based for-loop (this is a C++11 feature, at the time of writing it's supported by Microsoft Visual Studio 11 Beta, GCC 4.6 and Clang 3.0), you can use it to enumerate nodes/attributes. Additional helpers are provided to support this; note that they are also compatible with [@http://www.boost.org/libs/foreach/ Boost Foreach], and possibly other pre-C++11 foreach facilities. + + ``/implementation-defined type/`` xml_node::children() const; + ``/implementation-defined type/`` xml_node::children(const char_t* name) const; + ``/implementation-defined type/`` xml_node::attributes() const; + +`children` function allows you to enumerate all child nodes; `children` function with `name` argument allows you to enumerate all child nodes with a specific name; `attributes` function allows you to enumerate all attributes of the node. Note that you can also use node object itself in a range-based for construct, which is equivalent to using `children()`. + +This is an example of using these functions ([@samples/traverse_rangefor.cpp]): + +[import samples/traverse_rangefor.cpp] +[code_traverse_rangefor] + +[endsect] [/rangefor] + [section:iterators Traversing node/attribute lists via iterators] [#xml_node_iterator][#xml_attribute_iterator][#xml_node::begin][#xml_node::end][#xml_node::attributes_begin][#xml_node::attributes_end] @@ -2133,6 +2151,11 @@ Classes: * `xml_attribute `[link xml_node::last_attribute last_attribute]`() const;` [lbr] + * /implementation-defined type/ [link xml_node::children children]`() const;` + * /implementation-defined type/ [link xml_node::children children]`(const char_t* name) const;` + * /implementation-defined type/ [link xml_node::attributes attributes]`() const;` + [lbr] + * `xml_node `[link xml_node::child child]`(const char_t* name) const;` * `xml_attribute `[link xml_node::attribute attribute]`(const char_t* name) const;` * `xml_node `[link xml_node::next_sibling_name next_sibling]`(const char_t* name) const;` diff --git a/docs/samples/traverse_rangefor.cpp b/docs/samples/traverse_rangefor.cpp new file mode 100644 index 0000000..1f7212e --- /dev/null +++ b/docs/samples/traverse_rangefor.cpp @@ -0,0 +1,32 @@ +#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_rangefor + for (pugi::xml_node tool: tools.children("Tool")) + { + std::cout << "Tool:"; + + for (pugi::xml_attribute attr: tool.attributes()) + { + std::cout << " " << attr.name() << "=" << attr.value(); + } + + for (pugi::xml_node child: tool.children()) + { + std::cout << ", child " << child.name(); + } + + std::cout << std::endl; + } + //] +} + +// vim:et -- cgit v1.2.3