mirror of
https://github.com/zeux/pugixml.git
synced 2024-12-26 21:04:25 +08:00
docs: Added range-based for documentation and sample
git-svn-id: http://pugixml.googlecode.com/svn/trunk@904 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
parent
420a6fe9d5
commit
9b57b1bdd7
@ -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;`
|
||||
|
32
docs/samples/traverse_rangefor.cpp
Normal file
32
docs/samples/traverse_rangefor.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include "pugixml.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user