diff options
Diffstat (limited to 'docs/samples')
28 files changed, 1118 insertions, 1118 deletions
diff --git a/docs/samples/character.xml b/docs/samples/character.xml index b0f6f3f..078e0bf 100644 --- a/docs/samples/character.xml +++ b/docs/samples/character.xml @@ -1,8 +1,8 @@ -<?xml version="1.0"?>
-<network>
- <animation clip="idle" flags="loop" />
- <animation clip="run" flags="loop" />
- <animation clip="attack" />
-
- <?include transitions.xml?>
-</network>
+<?xml version="1.0"?> +<network> + <animation clip="idle" flags="loop" /> + <animation clip="run" flags="loop" /> + <animation clip="attack" /> + + <?include transitions.xml?> +</network> diff --git a/docs/samples/custom_memory_management.cpp b/docs/samples/custom_memory_management.cpp index 7e69528..92ccb71 100644 --- a/docs/samples/custom_memory_management.cpp +++ b/docs/samples/custom_memory_management.cpp @@ -1,27 +1,27 @@ -#include "pugixml.hpp"
-
-#include <new>
-
-//[code_custom_memory_management_decl
-void* custom_allocate(size_t size)
-{
- return new (std::nothrow) char[size];
-}
-
-void custom_deallocate(void* ptr)
-{
- delete[] static_cast<char*>(ptr);
-}
-//]
-
-int main()
-{
-//[code_custom_memory_management_call
- pugi::set_memory_management_functions(custom_allocate, custom_deallocate);
-//]
-
- pugi::xml_document doc;
- doc.load("<node/>");
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <new> + +//[code_custom_memory_management_decl +void* custom_allocate(size_t size) +{ + return new (std::nothrow) char[size]; +} + +void custom_deallocate(void* ptr) +{ + delete[] static_cast<char*>(ptr); +} +//] + +int main() +{ +//[code_custom_memory_management_call + pugi::set_memory_management_functions(custom_allocate, custom_deallocate); +//] + + pugi::xml_document doc; + doc.load("<node/>"); +} + +// vim:et diff --git a/docs/samples/include.cpp b/docs/samples/include.cpp index 0d80887..fa615a4 100644 --- a/docs/samples/include.cpp +++ b/docs/samples/include.cpp @@ -1,64 +1,64 @@ -#include "pugixml.hpp"
-
-#include <string.h>
-#include <iostream>
-
-//[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 <?include?>
-
- return result ? preprocess(doc) : false;
-}
-//]
-
-int main()
-{
- pugi::xml_document doc;
- if (!load_preprocess(doc, "character.xml")) return -1;
-
- doc.print(std::cout);
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <string.h> +#include <iostream> + +//[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 <?include?> + + return result ? preprocess(doc) : false; +} +//] + +int main() +{ + pugi::xml_document doc; + if (!load_preprocess(doc, "character.xml")) return -1; + + doc.print(std::cout); +} + +// vim:et diff --git a/docs/samples/load_error_handling.cpp b/docs/samples/load_error_handling.cpp index c7f44b8..18dd331 100644 --- a/docs/samples/load_error_handling.cpp +++ b/docs/samples/load_error_handling.cpp @@ -1,31 +1,31 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-void check_xml(const char* source)
-{
-//[code_load_error_handling
- pugi::xml_document doc;
- pugi::xml_parse_result result = doc.load(source);
-
- if (result)
- std::cout << "XML [" << source << "] parsed without errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n\n";
- else
- {
- std::cout << "XML [" << source << "] parsed with errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n";
- std::cout << "Error description: " << result.description() << "\n";
- std::cout << "Error offset: " << result.offset << " (error at [..." << (source + result.offset) << "]\n\n";
- }
-//]
-}
-
-int main()
-{
- check_xml("<node attr='value'><child>text</child></node>");
- check_xml("<node attr='value'><child>text</chil></node>");
- check_xml("<node attr='value'><child>text</child>");
- check_xml("<node attr='value\"><child>text</child></node>");
- check_xml("<node attr='value'><#tag /></node>");
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +void check_xml(const char* source) +{ +//[code_load_error_handling + pugi::xml_document doc; + pugi::xml_parse_result result = doc.load(source); + + if (result) + std::cout << "XML [" << source << "] parsed without errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n\n"; + else + { + std::cout << "XML [" << source << "] parsed with errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n"; + std::cout << "Error description: " << result.description() << "\n"; + std::cout << "Error offset: " << result.offset << " (error at [..." << (source + result.offset) << "]\n\n"; + } +//] +} + +int main() +{ + check_xml("<node attr='value'><child>text</child></node>"); + check_xml("<node attr='value'><child>text</chil></node>"); + check_xml("<node attr='value'><child>text</child>"); + check_xml("<node attr='value\"><child>text</child></node>"); + check_xml("<node attr='value'><#tag /></node>"); +} + +// vim:et diff --git a/docs/samples/load_file.cpp b/docs/samples/load_file.cpp index 3e77f04..f7b06c9 100644 --- a/docs/samples/load_file.cpp +++ b/docs/samples/load_file.cpp @@ -1,16 +1,16 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
-//[code_load_file
- pugi::xml_document doc;
-
- pugi::xml_parse_result result = doc.load_file("tree.xml");
-
- std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
-//]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ +//[code_load_file + pugi::xml_document doc; + + pugi::xml_parse_result result = doc.load_file("tree.xml"); + + std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl; +//] +} + +// vim:et diff --git a/docs/samples/load_memory.cpp b/docs/samples/load_memory.cpp index 67d5dee..365fb64 100644 --- a/docs/samples/load_memory.cpp +++ b/docs/samples/load_memory.cpp @@ -1,64 +1,64 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
-//[code_load_memory_decl
- const char source[] = "<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>";
- size_t size = sizeof(source);
-//]
-
- pugi::xml_document doc;
-
- {
- //[code_load_memory_buffer
- // You can use load_buffer to load document from immutable memory block:
- pugi::xml_parse_result result = doc.load_buffer(source, size);
- //]
-
- std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
- }
-
- {
- //[code_load_memory_buffer_inplace
- // You can use load_buffer_inplace to load document from mutable memory block; the block's lifetime must exceed that of document
- char* buffer = new char[size];
- memcpy(buffer, source, size);
-
- // The block can be allocated by any method; the block is modified during parsing
- pugi::xml_parse_result result = doc.load_buffer_inplace(buffer, size);
-
- //<-
- std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
- //->
- // You have to destroy the block yourself after the document is no longer used
- delete[] buffer;
- //]
- }
-
- {
- //[code_load_memory_buffer_inplace_own
- // You can use load_buffer_inplace_own to load document from mutable memory block and to pass the ownership of this block
- // The block has to be allocated via pugixml allocation function - using i.e. operator new here is incorrect
- char* buffer = static_cast<char*>(pugi::get_memory_allocation_function()(size));
- memcpy(buffer, source, size);
-
- // The block will be deleted by the document
- pugi::xml_parse_result result = doc.load_buffer_inplace_own(buffer, size);
- //]
-
- std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
- }
-
- {
- //[code_load_memory_string
- // You can use load to load document from null-terminated strings, for example literals:
- pugi::xml_parse_result result = doc.load("<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>");
- //]
-
- std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
- }
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ +//[code_load_memory_decl + const char source[] = "<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>"; + size_t size = sizeof(source); +//] + + pugi::xml_document doc; + + { + //[code_load_memory_buffer + // You can use load_buffer to load document from immutable memory block: + pugi::xml_parse_result result = doc.load_buffer(source, size); + //] + + std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl; + } + + { + //[code_load_memory_buffer_inplace + // You can use load_buffer_inplace to load document from mutable memory block; the block's lifetime must exceed that of document + char* buffer = new char[size]; + memcpy(buffer, source, size); + + // The block can be allocated by any method; the block is modified during parsing + pugi::xml_parse_result result = doc.load_buffer_inplace(buffer, size); + + //<- + std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl; + //-> + // You have to destroy the block yourself after the document is no longer used + delete[] buffer; + //] + } + + { + //[code_load_memory_buffer_inplace_own + // You can use load_buffer_inplace_own to load document from mutable memory block and to pass the ownership of this block + // The block has to be allocated via pugixml allocation function - using i.e. operator new here is incorrect + char* buffer = static_cast<char*>(pugi::get_memory_allocation_function()(size)); + memcpy(buffer, source, size); + + // The block will be deleted by the document + pugi::xml_parse_result result = doc.load_buffer_inplace_own(buffer, size); + //] + + std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl; + } + + { + //[code_load_memory_string + // You can use load to load document from null-terminated strings, for example literals: + pugi::xml_parse_result result = doc.load("<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>"); + //] + + std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl; + } +} + +// vim:et diff --git a/docs/samples/load_options.cpp b/docs/samples/load_options.cpp index c192914..04b4b46 100644 --- a/docs/samples/load_options.cpp +++ b/docs/samples/load_options.cpp @@ -1,30 +1,30 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
- pugi::xml_document doc;
-
-//[code_load_options
- const char* source = "<!--comment--><node><</node>";
-
- // Parsing with default options; note that comment node is not added to the tree, and entity reference < is expanded
- doc.load(source);
- std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n";
-
- // Parsing with additional parse_comments option; comment node is now added to the tree
- doc.load(source, pugi::parse_default | pugi::parse_comments);
- std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n";
-
- // Parsing with additional parse_comments option and without the (default) parse_escapes option; < is not expanded
- doc.load(source, (pugi::parse_default | pugi::parse_comments) & ~pugi::parse_escapes);
- std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n";
-
- // Parsing with minimal option mask; comment node is not added to the tree, and < is not expanded
- doc.load(source, pugi::parse_minimal);
- std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n";
-//]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ + pugi::xml_document doc; + +//[code_load_options + const char* source = "<!--comment--><node><</node>"; + + // Parsing with default options; note that comment node is not added to the tree, and entity reference < is expanded + doc.load(source); + std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; + + // Parsing with additional parse_comments option; comment node is now added to the tree + doc.load(source, pugi::parse_default | pugi::parse_comments); + std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; + + // Parsing with additional parse_comments option and without the (default) parse_escapes option; < is not expanded + doc.load(source, (pugi::parse_default | pugi::parse_comments) & ~pugi::parse_escapes); + std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; + + // Parsing with minimal option mask; comment node is not added to the tree, and < is not expanded + doc.load(source, pugi::parse_minimal); + std::cout << "First node value: [" << doc.first_child().value() << "], node child value: [" << doc.child_value("node") << "]\n"; +//] +} + +// vim:et diff --git a/docs/samples/load_stream.cpp b/docs/samples/load_stream.cpp index c717eeb..05cfb7f 100644 --- a/docs/samples/load_stream.cpp +++ b/docs/samples/load_stream.cpp @@ -1,97 +1,97 @@ -#include "pugixml.hpp"
-
-#include <fstream>
-#include <iomanip>
-#include <iostream>
-
-void print_doc(const char* message, const pugi::xml_document& doc, const pugi::xml_parse_result& result)
-{
- std::cout
- << message
- << "\t: load result '" << result.description() << "'"
- << ", first character of root name: U+" << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << pugi::as_wide(doc.first_child().name())[0]
- << ", year: " << doc.first_child().first_child().first_child().child_value()
- << std::endl;
-}
-
-bool try_imbue(std::wistream& stream, const char* name)
-{
- try
- {
- stream.imbue(std::locale(name));
-
- return true;
- }
- catch (const std::exception&)
- {
- return false;
- }
-}
-
-int main()
-{
- pugi::xml_document doc;
-
- {
- //[code_load_stream
- std::ifstream stream("weekly-utf-8.xml");
- pugi::xml_parse_result result = doc.load(stream);
- //]
-
- // first character of root name: U+9031, year: 1997
- print_doc("UTF8 file from narrow stream", doc, result);
- }
-
- {
- std::ifstream stream("weekly-utf-16.xml");
- pugi::xml_parse_result result = doc.load(stream);
-
- // first character of root name: U+9031, year: 1997
- print_doc("UTF16 file from narrow stream", doc, result);
- }
-
- {
- // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-8 file from a wide stream
- // directly if you have localized characters; you'll have to provide a UTF8 locale (there is no
- // standard one; you can use utf8_codecvt_facet from Boost or codecvt_utf8 from C++0x)
- std::wifstream stream("weekly-utf-8.xml");
-
- if (try_imbue(stream, "en_US.UTF-8")) // try Linux encoding
- {
- pugi::xml_parse_result result = doc.load(stream);
-
- // first character of root name: U+00E9, year: 1997
- print_doc("UTF8 file from wide stream", doc, result);
- }
- else
- {
- std::cout << "UTF-8 locale is not available\n";
- }
- }
-
- {
- // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-16 file from a wide stream without
- // using custom codecvt; you can use codecvt_utf16 from C++0x
- }
-
- {
- // Since encoding names are non-standard, you can't load the Shift-JIS (or any other non-ASCII) file
- // from a wide stream portably
- std::wifstream stream("weekly-shift_jis.xml");
-
- if (try_imbue(stream, ".932") || // try Microsoft encoding
- try_imbue(stream, "ja_JP.SJIS")) // try Linux encoding; run "localedef -i ja_JP -c -f SHIFT_JIS /usr/lib/locale/ja_JP.SJIS" to get it
- {
- pugi::xml_parse_result result = doc.load(stream);
-
- // first character of root name: U+9031, year: 1997
- print_doc("Shift-JIS file from wide stream", doc, result);
- }
- else
- {
- std::cout << "Shift-JIS locale is not available\n";
- }
- }
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <fstream> +#include <iomanip> +#include <iostream> + +void print_doc(const char* message, const pugi::xml_document& doc, const pugi::xml_parse_result& result) +{ + std::cout + << message + << "\t: load result '" << result.description() << "'" + << ", first character of root name: U+" << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << pugi::as_wide(doc.first_child().name())[0] + << ", year: " << doc.first_child().first_child().first_child().child_value() + << std::endl; +} + +bool try_imbue(std::wistream& stream, const char* name) +{ + try + { + stream.imbue(std::locale(name)); + + return true; + } + catch (const std::exception&) + { + return false; + } +} + +int main() +{ + pugi::xml_document doc; + + { + //[code_load_stream + std::ifstream stream("weekly-utf-8.xml"); + pugi::xml_parse_result result = doc.load(stream); + //] + + // first character of root name: U+9031, year: 1997 + print_doc("UTF8 file from narrow stream", doc, result); + } + + { + std::ifstream stream("weekly-utf-16.xml"); + pugi::xml_parse_result result = doc.load(stream); + + // first character of root name: U+9031, year: 1997 + print_doc("UTF16 file from narrow stream", doc, result); + } + + { + // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-8 file from a wide stream + // directly if you have localized characters; you'll have to provide a UTF8 locale (there is no + // standard one; you can use utf8_codecvt_facet from Boost or codecvt_utf8 from C++0x) + std::wifstream stream("weekly-utf-8.xml"); + + if (try_imbue(stream, "en_US.UTF-8")) // try Linux encoding + { + pugi::xml_parse_result result = doc.load(stream); + + // first character of root name: U+00E9, year: 1997 + print_doc("UTF8 file from wide stream", doc, result); + } + else + { + std::cout << "UTF-8 locale is not available\n"; + } + } + + { + // Since wide streams are treated as UTF-16/32 ones, you can't load the UTF-16 file from a wide stream without + // using custom codecvt; you can use codecvt_utf16 from C++0x + } + + { + // Since encoding names are non-standard, you can't load the Shift-JIS (or any other non-ASCII) file + // from a wide stream portably + std::wifstream stream("weekly-shift_jis.xml"); + + if (try_imbue(stream, ".932") || // try Microsoft encoding + try_imbue(stream, "ja_JP.SJIS")) // try Linux encoding; run "localedef -i ja_JP -c -f SHIFT_JIS /usr/lib/locale/ja_JP.SJIS" to get it + { + pugi::xml_parse_result result = doc.load(stream); + + // first character of root name: U+9031, year: 1997 + print_doc("Shift-JIS file from wide stream", doc, result); + } + else + { + std::cout << "Shift-JIS locale is not available\n"; + } + } +} + +// vim:et diff --git a/docs/samples/modify_add.cpp b/docs/samples/modify_add.cpp index 76619b3..8fecfc6 100644 --- a/docs/samples/modify_add.cpp +++ b/docs/samples/modify_add.cpp @@ -1,32 +1,32 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-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);
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +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); +} + +// vim:et diff --git a/docs/samples/modify_base.cpp b/docs/samples/modify_base.cpp index 7d52bd1..7d0959a 100644 --- a/docs/samples/modify_base.cpp +++ b/docs/samples/modify_base.cpp @@ -1,43 +1,43 @@ -#include "pugixml.hpp"
-
-#include <string.h>
-#include <iostream>
-
-int main()
-{
- pugi::xml_document doc;
- if (!doc.load("<node id='123'>text</node><!-- comment -->", 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;
- //]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <string.h> +#include <iostream> + +int main() +{ + pugi::xml_document doc; + if (!doc.load("<node id='123'>text</node><!-- comment -->", 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; + //] +} + +// vim:et diff --git a/docs/samples/modify_remove.cpp b/docs/samples/modify_remove.cpp index 015b0da..28c2f6b 100644 --- a/docs/samples/modify_remove.cpp +++ b/docs/samples/modify_remove.cpp @@ -1,27 +1,27 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
- pugi::xml_document doc;
- if (!doc.load("<node><description>Simple node</description><param name='id' value='123'/></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);
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ + pugi::xml_document doc; + if (!doc.load("<node><description>Simple node</description><param name='id' value='123'/></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); +} + +// vim:et diff --git a/docs/samples/save_custom_writer.cpp b/docs/samples/save_custom_writer.cpp index c0999e1..defcb33 100644 --- a/docs/samples/save_custom_writer.cpp +++ b/docs/samples/save_custom_writer.cpp @@ -1,116 +1,116 @@ -#include "pugixml.hpp"
-
-#include <string>
-
-#include <stdio.h>
-
-//[code_save_custom_writer
-struct xml_string_writer: pugi::xml_writer
-{
- std::string result;
-
- virtual void write(const void* data, size_t size)
- {
- result += std::string(static_cast<const char*>(data), size);
- }
-};
-//]
-
-struct xml_memory_writer: pugi::xml_writer
-{
- char* buffer;
- size_t capacity;
-
- size_t result;
-
- xml_memory_writer(): buffer(0), capacity(0), result(0)
- {
- }
-
- xml_memory_writer(char* buffer, size_t capacity): buffer(buffer), capacity(capacity), result(0)
- {
- }
-
- size_t written_size() const
- {
- return result < capacity ? result : capacity;
- }
-
- virtual void write(const void* data, size_t size)
- {
- if (result < capacity)
- {
- size_t chunk = (capacity - result < size) ? capacity - result : size;
-
- memcpy(buffer + result, data, chunk);
- }
-
- result += size;
- }
-};
-
-std::string node_to_string(pugi::xml_node node)
-{
- xml_string_writer writer;
- node.print(writer);
-
- return writer.result;
-}
-
-char* node_to_buffer(pugi::xml_node node, char* buffer, size_t size)
-{
- if (size == 0) return buffer;
-
- // leave one character for null terminator
- xml_memory_writer writer(buffer, size - 1);
- node.print(writer);
-
- // null terminate
- buffer[writer.written_size()] = 0;
-
- return buffer;
-}
-
-char* node_to_buffer_heap(pugi::xml_node node)
-{
- // first pass: get required memory size
- xml_memory_writer counter;
- node.print(counter);
-
- // allocate necessary size (+1 for null termination)
- char* buffer = new char[counter.result + 1];
-
- // second pass: actual printing
- xml_memory_writer writer(buffer, counter.result);
- node.print(writer);
-
- // null terminate
- buffer[writer.written_size()] = 0;
-
- return buffer;
-}
-
-int main()
-{
- // get a test document
- pugi::xml_document doc;
- doc.load("<foo bar='baz'>hey</foo>");
-
- // get contents as std::string (single pass)
- printf("contents: [%s]\n", node_to_string(doc).c_str());
-
- // get contents into fixed-size buffer (single pass)
- char large_buf[128];
- printf("contents: [%s]\n", node_to_buffer(doc, large_buf, sizeof(large_buf)));
-
- // get contents into fixed-size buffer (single pass, shows truncating behavior)
- char small_buf[22];
- printf("contents: [%s]\n", node_to_buffer(doc, small_buf, sizeof(small_buf)));
-
- // get contents into heap-allocated buffer (two passes)
- char* heap_buf = node_to_buffer_heap(doc);
- printf("contents: [%s]\n", heap_buf);
- delete[] heap_buf;
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <string> + +#include <stdio.h> + +//[code_save_custom_writer +struct xml_string_writer: pugi::xml_writer +{ + std::string result; + + virtual void write(const void* data, size_t size) + { + result += std::string(static_cast<const char*>(data), size); + } +}; +//] + +struct xml_memory_writer: pugi::xml_writer +{ + char* buffer; + size_t capacity; + + size_t result; + + xml_memory_writer(): buffer(0), capacity(0), result(0) + { + } + + xml_memory_writer(char* buffer, size_t capacity): buffer(buffer), capacity(capacity), result(0) + { + } + + size_t written_size() const + { + return result < capacity ? result : capacity; + } + + virtual void write(const void* data, size_t size) + { + if (result < capacity) + { + size_t chunk = (capacity - result < size) ? capacity - result : size; + + memcpy(buffer + result, data, chunk); + } + + result += size; + } +}; + +std::string node_to_string(pugi::xml_node node) +{ + xml_string_writer writer; + node.print(writer); + + return writer.result; +} + +char* node_to_buffer(pugi::xml_node node, char* buffer, size_t size) +{ + if (size == 0) return buffer; + + // leave one character for null terminator + xml_memory_writer writer(buffer, size - 1); + node.print(writer); + + // null terminate + buffer[writer.written_size()] = 0; + + return buffer; +} + +char* node_to_buffer_heap(pugi::xml_node node) +{ + // first pass: get required memory size + xml_memory_writer counter; + node.print(counter); + + // allocate necessary size (+1 for null termination) + char* buffer = new char[counter.result + 1]; + + // second pass: actual printing + xml_memory_writer writer(buffer, counter.result); + node.print(writer); + + // null terminate + buffer[writer.written_size()] = 0; + + return buffer; +} + +int main() +{ + // get a test document + pugi::xml_document doc; + doc.load("<foo bar='baz'>hey</foo>"); + + // get contents as std::string (single pass) + printf("contents: [%s]\n", node_to_string(doc).c_str()); + + // get contents into fixed-size buffer (single pass) + char large_buf[128]; + printf("contents: [%s]\n", node_to_buffer(doc, large_buf, sizeof(large_buf))); + + // get contents into fixed-size buffer (single pass, shows truncating behavior) + char small_buf[22]; + printf("contents: [%s]\n", node_to_buffer(doc, small_buf, sizeof(small_buf))); + + // get contents into heap-allocated buffer (two passes) + char* heap_buf = node_to_buffer_heap(doc); + printf("contents: [%s]\n", heap_buf); + delete[] heap_buf; +} + +// vim:et diff --git a/docs/samples/save_file.cpp b/docs/samples/save_file.cpp index c30e986..30c1aa1 100644 --- a/docs/samples/save_file.cpp +++ b/docs/samples/save_file.cpp @@ -1,17 +1,17 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
- // get a test document
- pugi::xml_document doc;
- doc.load("<foo bar='baz'>hey</foo>");
-
- //[code_save_file
- // save document to file
- std::cout << "Saving result: " << doc.save_file("save_file_output.xml") << std::endl;
- //]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ + // get a test document + pugi::xml_document doc; + doc.load("<foo bar='baz'>hey</foo>"); + + //[code_save_file + // save document to file + std::cout << "Saving result: " << doc.save_file("save_file_output.xml") << std::endl; + //] +} + +// vim:et diff --git a/docs/samples/save_options.cpp b/docs/samples/save_options.cpp index 45ac096..6a49f66 100644 --- a/docs/samples/save_options.cpp +++ b/docs/samples/save_options.cpp @@ -1,48 +1,48 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
- //[code_save_options
- // get a test document
- pugi::xml_document doc;
- doc.load("<foo bar='baz'><call>hey</call></foo>");
-
- // default options; prints
- // <?xml version="1.0"?>
- // <foo bar="baz">
- // <call>hey</call>
- // </foo>
- doc.save(std::cout);
- std::cout << std::endl;
-
- // default options with custom indentation string; prints
- // <?xml version="1.0"?>
- // <foo bar="baz">
- // --<call>hey</call>
- // </foo>
- doc.save(std::cout, "--");
- std::cout << std::endl;
-
- // default options without indentation; prints
- // <?xml version="1.0"?>
- // <foo bar="baz">
- // <call>hey</call>
- // </foo>
- doc.save(std::cout, "\t", pugi::format_default & ~pugi::format_indent); // can also pass "" instead of indentation string for the same effect
- std::cout << std::endl;
-
- // raw output; prints
- // <?xml version="1.0"?><foo bar="baz"><call>hey</call></foo>
- doc.save(std::cout, "\t", pugi::format_raw);
- std::cout << std::endl << std::endl;
-
- // raw output without declaration; prints
- // <foo bar="baz"><call>hey</call></foo>
- doc.save(std::cout, "\t", pugi::format_raw | pugi::format_no_declaration);
- std::cout << std::endl;
- //]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ + //[code_save_options + // get a test document + pugi::xml_document doc; + doc.load("<foo bar='baz'><call>hey</call></foo>"); + + // default options; prints + // <?xml version="1.0"?> + // <foo bar="baz"> + // <call>hey</call> + // </foo> + doc.save(std::cout); + std::cout << std::endl; + + // default options with custom indentation string; prints + // <?xml version="1.0"?> + // <foo bar="baz"> + // --<call>hey</call> + // </foo> + doc.save(std::cout, "--"); + std::cout << std::endl; + + // default options without indentation; prints + // <?xml version="1.0"?> + // <foo bar="baz"> + // <call>hey</call> + // </foo> + doc.save(std::cout, "\t", pugi::format_default & ~pugi::format_indent); // can also pass "" instead of indentation string for the same effect + std::cout << std::endl; + + // raw output; prints + // <?xml version="1.0"?><foo bar="baz"><call>hey</call></foo> + doc.save(std::cout, "\t", pugi::format_raw); + std::cout << std::endl << std::endl; + + // raw output without declaration; prints + // <foo bar="baz"><call>hey</call></foo> + doc.save(std::cout, "\t", pugi::format_raw | pugi::format_no_declaration); + std::cout << std::endl; + //] +} + +// vim:et diff --git a/docs/samples/save_stream.cpp b/docs/samples/save_stream.cpp index 39e99eb..d01965d 100644 --- a/docs/samples/save_stream.cpp +++ b/docs/samples/save_stream.cpp @@ -1,18 +1,18 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
- // get a test document
- pugi::xml_document doc;
- doc.load("<foo bar='baz'><call>hey</call></foo>");
-
- //[code_save_stream
- // save document to standard output
- std::cout << "Document:\n";
- doc.save(std::cout);
- //]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ + // get a test document + pugi::xml_document doc; + doc.load("<foo bar='baz'><call>hey</call></foo>"); + + //[code_save_stream + // save document to standard output + std::cout << "Document:\n"; + doc.save(std::cout); + //] +} + +// vim:et diff --git a/docs/samples/save_subtree.cpp b/docs/samples/save_subtree.cpp index a1c9f2f..0091b3d 100644 --- a/docs/samples/save_subtree.cpp +++ b/docs/samples/save_subtree.cpp @@ -1,26 +1,26 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
- //[code_save_subtree
- // get a test document
- pugi::xml_document doc;
- doc.load("<foo bar='baz'><call>hey</call></foo>");
-
- // print document to standard output (prints <?xml version="1.0"?><foo bar="baz"><call>hey</call></foo>)
- doc.save(std::cout, "", pugi::format_raw);
- std::cout << std::endl;
-
- // print document to standard output as a regular node (prints <foo bar="baz"><call>hey</call></foo>)
- doc.print(std::cout, "", pugi::format_raw);
- std::cout << std::endl;
-
- // print a subtree to standard output (prints <call>hey</call>)
- doc.child("foo").child("call").print(std::cout, "", pugi::format_raw);
- std::cout << std::endl;
- //]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ + //[code_save_subtree + // get a test document + pugi::xml_document doc; + doc.load("<foo bar='baz'><call>hey</call></foo>"); + + // print document to standard output (prints <?xml version="1.0"?><foo bar="baz"><call>hey</call></foo>) + doc.save(std::cout, "", pugi::format_raw); + std::cout << std::endl; + + // print document to standard output as a regular node (prints <foo bar="baz"><call>hey</call></foo>) + doc.print(std::cout, "", pugi::format_raw); + std::cout << std::endl; + + // print a subtree to standard output (prints <call>hey</call>) + doc.child("foo").child("call").print(std::cout, "", pugi::format_raw); + std::cout << std::endl; + //] +} + +// vim:et diff --git a/docs/samples/transitions.xml b/docs/samples/transitions.xml index 9c261fc..a195ef8 100644 --- a/docs/samples/transitions.xml +++ b/docs/samples/transitions.xml @@ -1,7 +1,7 @@ -<transition source="idle" target="run">
- <event name="key_up|key_shift" />
-</transition>
-<transition source="run" target="attack">
- <event name="key_ctrl" />
- <condition expr="weapon != null" />
-</transition>
+<transition source="idle" target="run"> + <event name="key_up|key_shift" /> +</transition> +<transition source="run" target="attack"> + <event name="key_ctrl" /> + <condition expr="weapon != null" /> +</transition> diff --git a/docs/samples/traverse_base.cpp b/docs/samples/traverse_base.cpp index b6f6d2e..d59c8b0 100644 --- a/docs/samples/traverse_base.cpp +++ b/docs/samples/traverse_base.cpp @@ -1,51 +1,51 @@ -#include "pugixml.hpp"
-
-#include <string.h>
-#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_base_basic
- for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling())
- {
- std::cout << "Tool:";
-
- for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute())
- {
- std::cout << " " << attr.name() << "=" << attr.value();
- }
-
- std::cout << std::endl;
- }
- //]
-
- std::cout << std::endl;
-
- //[code_traverse_base_data
- for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
- {
- std::cout << "Tool " << tool.attribute("Filename").value();
- std::cout << ": AllowRemote " << tool.attribute("AllowRemote").as_bool();
- std::cout << ", Timeout " << tool.attribute("Timeout").as_int();
- std::cout << ", Description '" << tool.child_value("Description") << "'\n";
- }
- //]
-
- std::cout << std::endl;
-
- //[code_traverse_base_contents
- std::cout << "Tool for *.dae generation: " << tools.find_child_by_attribute("Tool", "OutputFileMasks", "*.dae").attribute("Filename").value() << "\n";
-
- for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
- {
- std::cout << "Tool " << tool.attribute("Filename").value() << "\n";
- }
- //]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <string.h> +#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_base_basic + for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling()) + { + std::cout << "Tool:"; + + for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute()) + { + std::cout << " " << attr.name() << "=" << attr.value(); + } + + std::cout << std::endl; + } + //] + + std::cout << std::endl; + + //[code_traverse_base_data + for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool")) + { + std::cout << "Tool " << tool.attribute("Filename").value(); + std::cout << ": AllowRemote " << tool.attribute("AllowRemote").as_bool(); + std::cout << ", Timeout " << tool.attribute("Timeout").as_int(); + std::cout << ", Description '" << tool.child_value("Description") << "'\n"; + } + //] + + std::cout << std::endl; + + //[code_traverse_base_contents + std::cout << "Tool for *.dae generation: " << tools.find_child_by_attribute("Tool", "OutputFileMasks", "*.dae").attribute("Filename").value() << "\n"; + + for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool")) + { + std::cout << "Tool " << tool.attribute("Filename").value() << "\n"; + } + //] +} + +// vim:et diff --git a/docs/samples/traverse_iter.cpp b/docs/samples/traverse_iter.cpp index 935540f..90e0dc6 100644 --- a/docs/samples/traverse_iter.cpp +++ b/docs/samples/traverse_iter.cpp @@ -1,27 +1,27 @@ -#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;
- }
- //]
-}
-
-// vim:et
+#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; + } + //] +} + +// vim:et diff --git a/docs/samples/traverse_predicate.cpp b/docs/samples/traverse_predicate.cpp index e565597..9d8ded0 100644 --- a/docs/samples/traverse_predicate.cpp +++ b/docs/samples/traverse_predicate.cpp @@ -1,48 +1,48 @@ -#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;
- //]
-}
-
-// vim:et
+#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; + //] +} + +// vim:et diff --git a/docs/samples/traverse_walker.cpp b/docs/samples/traverse_walker.cpp index 9387fa0..cb99902 100644 --- a/docs/samples/traverse_walker.cpp +++ b/docs/samples/traverse_walker.cpp @@ -1,35 +1,35 @@ -#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);
- //]
-}
-
-// vim:et
+#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); + //] +} + +// vim:et diff --git a/docs/samples/tree.xml b/docs/samples/tree.xml index 81b6f4f..b33267a 100644 --- a/docs/samples/tree.xml +++ b/docs/samples/tree.xml @@ -1,12 +1,12 @@ -<?xml version="1.0"?>
-<mesh name="mesh_root">
- <!-- here is a mesh node -->
- some text
- <![CDATA[someothertext]]>
- some more text
- <node attr1="value1" attr2="value2" />
- <node attr1="value2">
- <innernode/>
- </node>
-</mesh>
-<?include somedata?>
+<?xml version="1.0"?> +<mesh name="mesh_root"> + <!-- here is a mesh node --> + some text + <![CDATA[someothertext]]> + some more text + <node attr1="value1" attr2="value2" /> + <node attr1="value2"> + <innernode/> + </node> +</mesh> +<?include somedata?> diff --git a/docs/samples/weekly-shift_jis.xml b/docs/samples/weekly-shift_jis.xml index 7421455..097e374 100644 --- a/docs/samples/weekly-shift_jis.xml +++ b/docs/samples/weekly-shift_jis.xml @@ -1,78 +1,78 @@ -<?xml version="1.0" encoding="Shift_JIS"?>
-<!DOCTYPE 週報 SYSTEM "weekly-shift_jis.dtd">
-<!-- 週報サンプル -->
-<週報>
- <年月週>
- <年度>1997</年度>
- <月度>1</月度>
- <週>1</週>
- </年月週>
-
- <氏名>
- <氏>山田</氏>
- <名>太郎</名>
- </氏名>
-
- <業務報告リスト>
- <業務報告>
- <業務名>XMLエディターの作成</業務名>
- <業務コード>X3355-23</業務コード>
- <工数管理>
- <見積もり工数>1600</見積もり工数>
- <実績工数>320</実績工数>
- <当月見積もり工数>160</当月見積もり工数>
- <当月実績工数>24</当月実績工数>
- </工数管理>
- <予定項目リスト>
- <予定項目>
- <P>XMLエディターの基本仕様の作成</P>
- </予定項目>
- </予定項目リスト>
- <実施事項リスト>
- <実施事項>
- <P>XMLエディターの基本仕様の作成</P>
- </実施事項>
- <実施事項>
- <P>競合他社製品の機能調査</P>
- </実施事項>
- </実施事項リスト>
- <上長への要請事項リスト>
- <上長への要請事項>
- <P>特になし</P>
- </上長への要請事項>
- </上長への要請事項リスト>
- <問題点対策>
- <P>XMLとは何かわからない。</P>
- </問題点対策>
- </業務報告>
-
- <業務報告>
- <業務名>検索エンジンの開発</業務名>
- <業務コード>S8821-76</業務コード>
- <工数管理>
- <見積もり工数>120</見積もり工数>
- <実績工数>6</実績工数>
- <当月見積もり工数>32</当月見積もり工数>
- <当月実績工数>2</当月実績工数>
- </工数管理>
- <予定項目リスト>
- <予定項目>
- <P><A href="http://www.goo.ne.jp">goo</A>の機能を調べてみる</P>
- </予定項目>
- </予定項目リスト>
- <実施事項リスト>
- <実施事項>
- <P>更に、どういう検索エンジンがあるか調査する</P>
- </実施事項>
- </実施事項リスト>
- <上長への要請事項リスト>
- <上長への要請事項>
- <P>開発をするのはめんどうなので、Yahoo!を買収して下さい。</P>
- </上長への要請事項>
- </上長への要請事項リスト>
- <問題点対策>
- <P>検索エンジンで車を走らせることができない。(要調査)</P>
- </問題点対策>
- </業務報告>
- </業務報告リスト>
-</週報>
+<?xml version="1.0" encoding="Shift_JIS"?> +<!DOCTYPE 週報 SYSTEM "weekly-shift_jis.dtd"> +<!-- 週報サンプル --> +<週報> + <年月週> + <年度>1997</年度> + <月度>1</月度> + <週>1</週> + </年月週> + + <氏名> + <氏>山田</氏> + <名>太郎</名> + </氏名> + + <業務報告リスト> + <業務報告> + <業務名>XMLエディターの作成</業務名> + <業務コード>X3355-23</業務コード> + <工数管理> + <見積もり工数>1600</見積もり工数> + <実績工数>320</実績工数> + <当月見積もり工数>160</当月見積もり工数> + <当月実績工数>24</当月実績工数> + </工数管理> + <予定項目リスト> + <予定項目> + <P>XMLエディターの基本仕様の作成</P> + </予定項目> + </予定項目リスト> + <実施事項リスト> + <実施事項> + <P>XMLエディターの基本仕様の作成</P> + </実施事項> + <実施事項> + <P>競合他社製品の機能調査</P> + </実施事項> + </実施事項リスト> + <上長への要請事項リスト> + <上長への要請事項> + <P>特になし</P> + </上長への要請事項> + </上長への要請事項リスト> + <問題点対策> + <P>XMLとは何かわからない。</P> + </問題点対策> + </業務報告> + + <業務報告> + <業務名>検索エンジンの開発</業務名> + <業務コード>S8821-76</業務コード> + <工数管理> + <見積もり工数>120</見積もり工数> + <実績工数>6</実績工数> + <当月見積もり工数>32</当月見積もり工数> + <当月実績工数>2</当月実績工数> + </工数管理> + <予定項目リスト> + <予定項目> + <P><A href="http://www.goo.ne.jp">goo</A>の機能を調べてみる</P> + </予定項目> + </予定項目リスト> + <実施事項リスト> + <実施事項> + <P>更に、どういう検索エンジンがあるか調査する</P> + </実施事項> + </実施事項リスト> + <上長への要請事項リスト> + <上長への要請事項> + <P>開発をするのはめんどうなので、Yahoo!を買収して下さい。</P> + </上長への要請事項> + </上長への要請事項リスト> + <問題点対策> + <P>検索エンジンで車を走らせることができない。(要調査)</P> + </問題点対策> + </業務報告> + </業務報告リスト> +</週報> diff --git a/docs/samples/weekly-utf-8.xml b/docs/samples/weekly-utf-8.xml index 497f572..d55474e 100644 --- a/docs/samples/weekly-utf-8.xml +++ b/docs/samples/weekly-utf-8.xml @@ -1,78 +1,78 @@ -<?xml version="1.0"?>
-<!DOCTYPE 騾ア蝣ア SYSTEM "weekly-utf-8.dtd">
-<!-- 騾ア蝣ア繧オ繝ウ繝励Ν -->
-<騾ア蝣ア>
- <蟷エ譛磯ア>
- <蟷エ蠎ヲ>1997</蟷エ蠎ヲ>
- <譛亥コヲ>1</譛亥コヲ>
- <騾ア>1</騾ア>
- </蟷エ譛磯ア>
-
- <豌丞錐>
- <豌>螻ア逕ー</豌>
- <蜷>螟ェ驛</蜷>
- </豌丞錐>
-
- <讌ュ蜍吝ア蜻翫Μ繧ケ繝>
- <讌ュ蜍吝ア蜻>
- <讌ュ蜍吝錐>XML繧ィ繝繧」繧ソ繝シ縺ョ菴懈</讌ュ蜍吝錐>
- <讌ュ蜍吶さ繝シ繝>X3355-23</讌ュ蜍吶さ繝シ繝>
- <蟾・謨ー邂。逅>
- <隕狗ゥ阪b繧雁キ・謨ー>1600</隕狗ゥ阪b繧雁キ・謨ー>
- <螳溽クセ蟾・謨ー>320</螳溽クセ蟾・謨ー>
- <蠖捺怦隕狗ゥ阪b繧雁キ・謨ー>160</蠖捺怦隕狗ゥ阪b繧雁キ・謨ー>
- <蠖捺怦螳溽クセ蟾・謨ー>24</蠖捺怦螳溽クセ蟾・謨ー>
- </蟾・謨ー邂。逅>
- <莠亥ョ夐逶ョ繝ェ繧ケ繝>
- <莠亥ョ夐逶ョ>
- <P>XML繧ィ繝繧」繧ソ繝シ縺ョ蝓コ譛ャ莉墓ァ倥ョ菴懈</P>
- </莠亥ョ夐逶ョ>
- </莠亥ョ夐逶ョ繝ェ繧ケ繝>
- <螳滓命莠矩繝ェ繧ケ繝>
- <螳滓命莠矩>
- <P>XML繧ィ繝繧」繧ソ繝シ縺ョ蝓コ譛ャ莉墓ァ倥ョ菴懈</P>
- </螳滓命莠矩>
- <螳滓命莠矩>
- <P>遶カ蜷井サ也、セ陬ス蜩√ョ讖溯ス隱ソ譟サ</P>
- </螳滓命莠矩>
- </螳滓命莠矩繝ェ繧ケ繝>
- <荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝>
- <荳企聞縺ク縺ョ隕∬ォ倶コ矩>
- <P>迚ケ縺ォ縺ェ縺</P>
- </荳企聞縺ク縺ョ隕∬ォ倶コ矩>
- </荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝>
- <蝠城。檎せ蟇セ遲>
- <P>XML縺ィ縺ッ菴輔°繧上°繧峨↑縺縲</P>
- </蝠城。檎せ蟇セ遲>
- </讌ュ蜍吝ア蜻>
-
- <讌ュ蜍吝ア蜻>
- <讌ュ蜍吝錐>讀懃エ「繧ィ繝ウ繧ク繝ウ縺ョ髢狗匱</讌ュ蜍吝錐>
- <讌ュ蜍吶さ繝シ繝>S8821-76</讌ュ蜍吶さ繝シ繝>
- <蟾・謨ー邂。逅>
- <隕狗ゥ阪b繧雁キ・謨ー>120</隕狗ゥ阪b繧雁キ・謨ー>
- <螳溽クセ蟾・謨ー>6</螳溽クセ蟾・謨ー>
- <蠖捺怦隕狗ゥ阪b繧雁キ・謨ー>32</蠖捺怦隕狗ゥ阪b繧雁キ・謨ー>
- <蠖捺怦螳溽クセ蟾・謨ー>2</蠖捺怦螳溽クセ蟾・謨ー>
- </蟾・謨ー邂。逅>
- <莠亥ョ夐逶ョ繝ェ繧ケ繝>
- <莠亥ョ夐逶ョ>
- <P><A href="http://www.goo.ne.jp">goo</A>縺ョ讖溯ス繧定ェソ縺ケ縺ヲ縺ソ繧</P>
- </莠亥ョ夐逶ョ>
- </莠亥ョ夐逶ョ繝ェ繧ケ繝>
- <螳滓命莠矩繝ェ繧ケ繝>
- <螳滓命莠矩>
- <P>譖エ縺ォ縲√←縺縺縺讀懃エ「繧ィ繝ウ繧ク繝ウ縺後≠繧九°隱ソ譟サ縺吶k</P>
- </螳滓命莠矩>
- </螳滓命莠矩繝ェ繧ケ繝>
- <荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝>
- <荳企聞縺ク縺ョ隕∬ォ倶コ矩>
- <P>髢狗匱繧偵☆繧九ョ縺ッ繧√s縺ゥ縺縺ェ縺ョ縺ァ縲〆ahoo!繧定イキ蜿弱@縺ヲ荳九&縺縲</P>
- </荳企聞縺ク縺ョ隕∬ォ倶コ矩>
- </荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝>
- <蝠城。檎せ蟇セ遲>
- <P>讀懃エ「繧ィ繝ウ繧ク繝ウ縺ァ霆翫r襍ー繧峨○繧九%縺ィ縺後〒縺阪↑縺縲ゑシ郁ヲ∬ェソ譟サシ</P>
- </蝠城。檎せ蟇セ遲>
- </讌ュ蜍吝ア蜻>
- </讌ュ蜍吝ア蜻翫Μ繧ケ繝>
-</騾ア蝣ア>
+<?xml version="1.0"?> +<!DOCTYPE 騾ア蝣ア SYSTEM "weekly-utf-8.dtd"> +<!-- 騾ア蝣ア繧オ繝ウ繝励Ν --> +<騾ア蝣ア> + <蟷エ譛磯ア> + <蟷エ蠎ヲ>1997</蟷エ蠎ヲ> + <譛亥コヲ>1</譛亥コヲ> + <騾ア>1</騾ア> + </蟷エ譛磯ア> + + <豌丞錐> + <豌>螻ア逕ー</豌> + <蜷>螟ェ驛</蜷> + </豌丞錐> + + <讌ュ蜍吝ア蜻翫Μ繧ケ繝> + <讌ュ蜍吝ア蜻> + <讌ュ蜍吝錐>XML繧ィ繝繧」繧ソ繝シ縺ョ菴懈</讌ュ蜍吝錐> + <讌ュ蜍吶さ繝シ繝>X3355-23</讌ュ蜍吶さ繝シ繝> + <蟾・謨ー邂。逅> + <隕狗ゥ阪b繧雁キ・謨ー>1600</隕狗ゥ阪b繧雁キ・謨ー> + <螳溽クセ蟾・謨ー>320</螳溽クセ蟾・謨ー> + <蠖捺怦隕狗ゥ阪b繧雁キ・謨ー>160</蠖捺怦隕狗ゥ阪b繧雁キ・謨ー> + <蠖捺怦螳溽クセ蟾・謨ー>24</蠖捺怦螳溽クセ蟾・謨ー> + </蟾・謨ー邂。逅> + <莠亥ョ夐逶ョ繝ェ繧ケ繝> + <莠亥ョ夐逶ョ> + <P>XML繧ィ繝繧」繧ソ繝シ縺ョ蝓コ譛ャ莉墓ァ倥ョ菴懈</P> + </莠亥ョ夐逶ョ> + </莠亥ョ夐逶ョ繝ェ繧ケ繝> + <螳滓命莠矩繝ェ繧ケ繝> + <螳滓命莠矩> + <P>XML繧ィ繝繧」繧ソ繝シ縺ョ蝓コ譛ャ莉墓ァ倥ョ菴懈</P> + </螳滓命莠矩> + <螳滓命莠矩> + <P>遶カ蜷井サ也、セ陬ス蜩√ョ讖溯ス隱ソ譟サ</P> + </螳滓命莠矩> + </螳滓命莠矩繝ェ繧ケ繝> + <荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝> + <荳企聞縺ク縺ョ隕∬ォ倶コ矩> + <P>迚ケ縺ォ縺ェ縺</P> + </荳企聞縺ク縺ョ隕∬ォ倶コ矩> + </荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝> + <蝠城。檎せ蟇セ遲> + <P>XML縺ィ縺ッ菴輔°繧上°繧峨↑縺縲</P> + </蝠城。檎せ蟇セ遲> + </讌ュ蜍吝ア蜻> + + <讌ュ蜍吝ア蜻> + <讌ュ蜍吝錐>讀懃エ「繧ィ繝ウ繧ク繝ウ縺ョ髢狗匱</讌ュ蜍吝錐> + <讌ュ蜍吶さ繝シ繝>S8821-76</讌ュ蜍吶さ繝シ繝> + <蟾・謨ー邂。逅> + <隕狗ゥ阪b繧雁キ・謨ー>120</隕狗ゥ阪b繧雁キ・謨ー> + <螳溽クセ蟾・謨ー>6</螳溽クセ蟾・謨ー> + <蠖捺怦隕狗ゥ阪b繧雁キ・謨ー>32</蠖捺怦隕狗ゥ阪b繧雁キ・謨ー> + <蠖捺怦螳溽クセ蟾・謨ー>2</蠖捺怦螳溽クセ蟾・謨ー> + </蟾・謨ー邂。逅> + <莠亥ョ夐逶ョ繝ェ繧ケ繝> + <莠亥ョ夐逶ョ> + <P><A href="http://www.goo.ne.jp">goo</A>縺ョ讖溯ス繧定ェソ縺ケ縺ヲ縺ソ繧</P> + </莠亥ョ夐逶ョ> + </莠亥ョ夐逶ョ繝ェ繧ケ繝> + <螳滓命莠矩繝ェ繧ケ繝> + <螳滓命莠矩> + <P>譖エ縺ォ縲√←縺縺縺讀懃エ「繧ィ繝ウ繧ク繝ウ縺後≠繧九°隱ソ譟サ縺吶k</P> + </螳滓命莠矩> + </螳滓命莠矩繝ェ繧ケ繝> + <荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝> + <荳企聞縺ク縺ョ隕∬ォ倶コ矩> + <P>髢狗匱繧偵☆繧九ョ縺ッ繧√s縺ゥ縺縺ェ縺ョ縺ァ縲〆ahoo!繧定イキ蜿弱@縺ヲ荳九&縺縲</P> + </荳企聞縺ク縺ョ隕∬ォ倶コ矩> + </荳企聞縺ク縺ョ隕∬ォ倶コ矩繝ェ繧ケ繝> + <蝠城。檎せ蟇セ遲> + <P>讀懃エ「繧ィ繝ウ繧ク繝ウ縺ァ霆翫r襍ー繧峨○繧九%縺ィ縺後〒縺阪↑縺縲ゑシ郁ヲ∬ェソ譟サシ</P> + </蝠城。檎せ蟇セ遲> + </讌ュ蜍吝ア蜻> + </讌ュ蜍吝ア蜻翫Μ繧ケ繝> +</騾ア蝣ア> diff --git a/docs/samples/xgconsole.xml b/docs/samples/xgconsole.xml index cf603ad..b2bf5a2 100644 --- a/docs/samples/xgconsole.xml +++ b/docs/samples/xgconsole.xml @@ -1,12 +1,12 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
-<Profile FormatVersion="1">
- <Tools>
- <Tool Filename="jam" AllowIntercept="true">
- <Description>Jamplus build system</Description>
- </Tool>
- <Tool Filename="mayabatch.exe" AllowRemote="true" OutputFileMasks="*.dae" DeriveCaptionFrom="lastparam" Timeout="40" />
- <Tool Filename="meshbuilder_*.exe" AllowRemote="false" OutputFileMasks="*.mesh" DeriveCaptionFrom="lastparam" Timeout="10" />
- <Tool Filename="texbuilder_*.exe" AllowRemote="true" OutputFileMasks="*.tex" DeriveCaptionFrom="lastparam" />
- <Tool Filename="shaderbuilder_*.exe" AllowRemote="true" DeriveCaptionFrom="lastparam" />
- </Tools>
-</Profile>
+<?xml version="1.0" encoding="UTF-8" standalone="no" ?> +<Profile FormatVersion="1"> + <Tools> + <Tool Filename="jam" AllowIntercept="true"> + <Description>Jamplus build system</Description> + </Tool> + <Tool Filename="mayabatch.exe" AllowRemote="true" OutputFileMasks="*.dae" DeriveCaptionFrom="lastparam" Timeout="40" /> + <Tool Filename="meshbuilder_*.exe" AllowRemote="false" OutputFileMasks="*.mesh" DeriveCaptionFrom="lastparam" Timeout="10" /> + <Tool Filename="texbuilder_*.exe" AllowRemote="true" OutputFileMasks="*.tex" DeriveCaptionFrom="lastparam" /> + <Tool Filename="shaderbuilder_*.exe" AllowRemote="true" DeriveCaptionFrom="lastparam" /> + </Tools> +</Profile> diff --git a/docs/samples/xpath_error.cpp b/docs/samples/xpath_error.cpp index 3415d31..6cb6f4f 100644 --- a/docs/samples/xpath_error.cpp +++ b/docs/samples/xpath_error.cpp @@ -1,43 +1,43 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
- pugi::xml_document doc;
- if (!doc.load_file("xgconsole.xml")) return -1;
-
-//[code_xpath_error
- // Exception is thrown for incorrect query syntax
- try
- {
- doc.select_nodes("//nodes[#true()]");
- }
- catch (const pugi::xpath_exception& e)
- {
- std::cout << "Select failed: " << e.what() << std::endl;
- }
-
- // Exception is thrown for incorrect query semantics
- try
- {
- doc.select_nodes("(123)/next");
- }
- catch (const pugi::xpath_exception& e)
- {
- std::cout << "Select failed: " << e.what() << std::endl;
- }
-
- // Exception is thrown for query with incorrect return type
- try
- {
- doc.select_nodes("123");
- }
- catch (const pugi::xpath_exception& e)
- {
- std::cout << "Select failed: " << e.what() << std::endl;
- }
-//]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ + pugi::xml_document doc; + if (!doc.load_file("xgconsole.xml")) return -1; + +//[code_xpath_error + // Exception is thrown for incorrect query syntax + try + { + doc.select_nodes("//nodes[#true()]"); + } + catch (const pugi::xpath_exception& e) + { + std::cout << "Select failed: " << e.what() << std::endl; + } + + // Exception is thrown for incorrect query semantics + try + { + doc.select_nodes("(123)/next"); + } + catch (const pugi::xpath_exception& e) + { + std::cout << "Select failed: " << e.what() << std::endl; + } + + // Exception is thrown for query with incorrect return type + try + { + doc.select_nodes("123"); + } + catch (const pugi::xpath_exception& e) + { + std::cout << "Select failed: " << e.what() << std::endl; + } +//] +} + +// vim:et diff --git a/docs/samples/xpath_query.cpp b/docs/samples/xpath_query.cpp index b14a0b0..c622a9c 100644 --- a/docs/samples/xpath_query.cpp +++ b/docs/samples/xpath_query.cpp @@ -1,36 +1,36 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-#include <string>
-
-int main()
-{
- pugi::xml_document doc;
- if (!doc.load_file("xgconsole.xml")) return -1;
-
-//[code_xpath_query
- // Select nodes via compiled query
- pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']");
-
- pugi::xpath_node_set tools = query_remote_tools.evaluate_node_set(doc);
- std::cout << "Remote tool: ";
- tools[2].node().print(std::cout);
-
- // Evaluate numbers via compiled query
- pugi::xpath_query query_timeouts("sum(//Tool/@Timeout)");
- std::cout << query_timeouts.evaluate_number(doc) << std::endl;
-
- // Evaluate strings via compiled query for different context nodes
- pugi::xpath_query query_name_valid("string-length(substring-before(@Filename, '_')) > 0 and @OutputFileMasks");
- pugi::xpath_query query_name("concat(substring-before(@Filename, '_'), ' produces ', @OutputFileMasks)");
-
- for (pugi::xml_node tool = doc.first_element_by_path("Profile/Tools/Tool"); tool; tool = tool.next_sibling())
- {
- std::string s = query_name.evaluate_string(tool);
-
- if (query_name_valid.evaluate_boolean(tool)) std::cout << s << std::endl;
- }
-//]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> +#include <string> + +int main() +{ + pugi::xml_document doc; + if (!doc.load_file("xgconsole.xml")) return -1; + +//[code_xpath_query + // Select nodes via compiled query + pugi::xpath_query query_remote_tools("/Profile/Tools/Tool[@AllowRemote='true']"); + + pugi::xpath_node_set tools = query_remote_tools.evaluate_node_set(doc); + std::cout << "Remote tool: "; + tools[2].node().print(std::cout); + + // Evaluate numbers via compiled query + pugi::xpath_query query_timeouts("sum(//Tool/@Timeout)"); + std::cout << query_timeouts.evaluate_number(doc) << std::endl; + + // Evaluate strings via compiled query for different context nodes + pugi::xpath_query query_name_valid("string-length(substring-before(@Filename, '_')) > 0 and @OutputFileMasks"); + pugi::xpath_query query_name("concat(substring-before(@Filename, '_'), ' produces ', @OutputFileMasks)"); + + for (pugi::xml_node tool = doc.first_element_by_path("Profile/Tools/Tool"); tool; tool = tool.next_sibling()) + { + std::string s = query_name.evaluate_string(tool); + + if (query_name_valid.evaluate_boolean(tool)) std::cout << s << std::endl; + } +//] +} + +// vim:et diff --git a/docs/samples/xpath_select.cpp b/docs/samples/xpath_select.cpp index 5645fd3..c098bd1 100644 --- a/docs/samples/xpath_select.cpp +++ b/docs/samples/xpath_select.cpp @@ -1,27 +1,27 @@ -#include "pugixml.hpp"
-
-#include <iostream>
-
-int main()
-{
- pugi::xml_document doc;
- if (!doc.load_file("xgconsole.xml")) return -1;
-
-//[code_xpath_select
- pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']");
-
- std::cout << "Tools:";
-
- for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it)
- {
- pugi::xpath_node node = *it;
- std::cout << " " << node.node().attribute("Filename").value();
- }
-
- pugi::xpath_node build_tool = doc.select_single_node("//Tool[contains(Description, 'build system')]");
-
- std::cout << "\nBuild tool: " << build_tool.node().attribute("Filename").value() << "\n";
-//]
-}
-
-// vim:et
+#include "pugixml.hpp" + +#include <iostream> + +int main() +{ + pugi::xml_document doc; + if (!doc.load_file("xgconsole.xml")) return -1; + +//[code_xpath_select + pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']"); + + std::cout << "Tools:"; + + for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it) + { + pugi::xpath_node node = *it; + std::cout << " " << node.node().attribute("Filename").value(); + } + + pugi::xpath_node build_tool = doc.select_single_node("//Tool[contains(Description, 'build system')]"); + + std::cout << "\nBuild tool: " << build_tool.node().attribute("Filename").value() << "\n"; +//] +} + +// vim:et |