summaryrefslogtreecommitdiff
path: root/docs/samples
diff options
context:
space:
mode:
Diffstat (limited to 'docs/samples')
-rw-r--r--docs/samples/traverse_iter.cpp25
-rw-r--r--docs/samples/traverse_predicate.cpp46
-rw-r--r--docs/samples/traverse_walker.cpp33
-rw-r--r--docs/samples/tree.xml2
4 files changed, 105 insertions, 1 deletions
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 <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_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 <string.h>
+#include <iostream>
+
+//[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 <iostream>
+
+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 @@
<mesh name="mesh_root">
<!-- here is a mesh node -->
some text
- <![CDATA[[someothertext]]>
+ <![CDATA[someothertext]]>
some more text
<node attr1="value1" attr2="value2" />
<node attr1="value2">