From 6d0f60a808f419d6acaf54f020b2d53a00d843ba Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Thu, 8 Jul 2010 04:34:03 +0000 Subject: docs: Added document modification documentation git-svn-id: http://pugixml.googlecode.com/svn/trunk@574 99668b35-9821-0410-8761-19e4c4f06640 --- docs/samples/character.xml | 8 ++++++ docs/samples/include.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++ docs/samples/modify_add.cpp | 30 ++++++++++++++++++++ docs/samples/modify_base.cpp | 41 ++++++++++++++++++++++++++++ docs/samples/modify_remove.cpp | 25 +++++++++++++++++ docs/samples/transitions.xml | 7 +++++ 6 files changed, 173 insertions(+) create mode 100644 docs/samples/character.xml create mode 100644 docs/samples/include.cpp create mode 100644 docs/samples/modify_add.cpp create mode 100644 docs/samples/modify_base.cpp create mode 100644 docs/samples/modify_remove.cpp create mode 100644 docs/samples/transitions.xml (limited to 'docs/samples') diff --git a/docs/samples/character.xml b/docs/samples/character.xml new file mode 100644 index 0000000..b0f6f3f --- /dev/null +++ b/docs/samples/character.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/docs/samples/include.cpp b/docs/samples/include.cpp new file mode 100644 index 0000000..528d016 --- /dev/null +++ b/docs/samples/include.cpp @@ -0,0 +1,62 @@ +#include "pugixml.hpp" + +#include +#include + +//[code_include +bool load_preprocess(pugi::xml_document& doc, const char* path); + +bool preprocess(pugi::xml_node node) +{ + for (pugi::xml_node child = node.first_child(); child; ) + { + if (child.type() == pugi::node_pi && strcmp(child.name(), "include") == 0) + { + pugi::xml_node include = child; + + // load new preprocessed document (note: ideally this should handle relative paths) + const char* path = include.value(); + + pugi::xml_document doc; + if (!load_preprocess(doc, path)) return false; + + // insert the comment marker above include directive + node.insert_child_before(pugi::node_comment, include).set_value(path); + + // copy the document above the include directive (this retains the original order!) + for (pugi::xml_node ic = doc.first_child(); ic; ic = ic.next_sibling()) + { + node.insert_copy_before(ic, include); + } + + // remove the include node and move to the next child + child = child.next_sibling(); + + node.remove_child(include); + } + else + { + if (!preprocess(child)) return false; + + child = child.next_sibling(); + } + } + + return true; +} + +bool load_preprocess(pugi::xml_document& doc, const char* path) +{ + pugi::xml_parse_result result = doc.load_file(path, pugi::parse_default | pugi::parse_pi); // for + + return result ? preprocess(doc) : false; +} +//] + +int main() +{ + pugi::xml_document doc; + if (!load_preprocess(doc, "character.xml")) return -1; + + doc.print(std::cout); +} diff --git a/docs/samples/modify_add.cpp b/docs/samples/modify_add.cpp new file mode 100644 index 0000000..88c8fe6 --- /dev/null +++ b/docs/samples/modify_add.cpp @@ -0,0 +1,30 @@ +#include "pugixml.hpp" + +#include + +int main() +{ + pugi::xml_document doc; + + //[code_modify_add + // add node with some name + pugi::xml_node node = doc.append_child(); + node.set_name("node"); + + // add description node with text child + pugi::xml_node descr = node.append_child(); + descr.set_name("description"); + descr.append_child(pugi::node_pcdata).set_value("Simple node"); + + // add param node before the description + pugi::xml_node param = node.insert_child_before(pugi::node_element, descr); + param.set_name("param"); + + // add attributes to param node + param.append_attribute("name") = "version"; + param.append_attribute("value") = 1.1; + param.insert_attribute_after("type", param.attribute("name")) = "float"; + //] + + doc.print(std::cout); +} diff --git a/docs/samples/modify_base.cpp b/docs/samples/modify_base.cpp new file mode 100644 index 0000000..4213cff --- /dev/null +++ b/docs/samples/modify_base.cpp @@ -0,0 +1,41 @@ +#include "pugixml.hpp" + +#include +#include + +int main() +{ + pugi::xml_document doc; + if (!doc.load("text", pugi::parse_default | pugi::parse_comments)) return -1; + + //[code_modify_base_node + pugi::xml_node node = doc.child("node"); + + // change node name + std::cout << node.set_name("notnode"); + std::cout << ", new node name: " << node.name() << std::endl; + + // change comment text + std::cout << doc.last_child().set_value("useless comment"); + std::cout << ", new comment text: " << doc.last_child().value() << std::endl; + + // we can't change value of the element or name of the comment + std::cout << node.set_value("1") << ", " << doc.last_child().set_name("2") << std::endl; + //] + + //[code_modify_base_attr + pugi::xml_attribute attr = node.attribute("id"); + + // change attribute name/value + std::cout << attr.set_name("key") << ", " << attr.set_value("345"); + std::cout << ", new attribute: " << attr.name() << "=" << attr.value() << std::endl; + + // we can use numbers or booleans + attr.set_value(1.234); + std::cout << "new attribute value: " << attr.value() << std::endl; + + // we can also use assignment operators for more concise code + attr = true; + std::cout << "final attribute value: " << attr.value() << std::endl; + //] +} diff --git a/docs/samples/modify_remove.cpp b/docs/samples/modify_remove.cpp new file mode 100644 index 0000000..21dcc32 --- /dev/null +++ b/docs/samples/modify_remove.cpp @@ -0,0 +1,25 @@ +#include "pugixml.hpp" + +#include + +int main() +{ + pugi::xml_document doc; + if (!doc.load("Simple node")) return -1; + + //[code_modify_remove + // remove description node with the whole subtree + pugi::xml_node node = doc.child("node"); + node.remove_child("description"); + + // remove id attribute + pugi::xml_node param = node.child("param"); + param.remove_attribute("value"); + + // we can also remove nodes/attributes by handles + pugi::xml_attribute id = param.attribute("name"); + param.remove_attribute(id); + //] + + doc.print(std::cout); +} diff --git a/docs/samples/transitions.xml b/docs/samples/transitions.xml new file mode 100644 index 0000000..9c261fc --- /dev/null +++ b/docs/samples/transitions.xml @@ -0,0 +1,7 @@ + + + + + + + -- cgit v1.2.3