From 7e7153457722ff2272bf094cca5387bd2e2ebd71 Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine@gmail.com" Date: Sun, 29 Apr 2012 22:51:21 +0000 Subject: docs: Regenerated HTML documentation git-svn-id: http://pugixml.googlecode.com/svn/trunk@908 99668b35-9821-0410-8761-19e4c4f06640 --- docs/manual.html | 14 +- docs/manual/access.html | 208 +++++++++++++++-- docs/manual/apiref.html | 182 ++++++++++++++- docs/manual/changes.html | 104 ++++++++- docs/manual/dom.html | 70 +++++- docs/manual/install.html | 88 +++++++- docs/manual/loading.html | 44 +++- docs/manual/modify.html | 90 +++++++- docs/manual/saving.html | 110 +++++++-- docs/manual/toc.html | 17 +- docs/manual/xpath.html | 16 +- docs/quickstart.html | 567 +++++++++++++++-------------------------------- 12 files changed, 1017 insertions(+), 493 deletions(-) diff --git a/docs/manual.html b/docs/manual.html index 2218160..66efba2 100644 --- a/docs/manual.html +++ b/docs/manual.html @@ -1,16 +1,16 @@ -pugixml 1.0 +pugixml 1.2 - +
-pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -147,7 +147,7 @@

- Copyright (c) 2006-2010 Arseny Kapoulkine + Copyright (c) 2006-2012 Arseny Kapoulkine

Permission is hereby granted, free of charge, to any person obtaining a @@ -179,18 +179,18 @@

This software is based on pugixml library (http://pugixml.org).
pugixml - is Copyright (C) 2006-2010 Arseny Kapoulkine. + is Copyright (C) 2006-2012 Arseny Kapoulkine.

- +

Last revised: October 31, 2010 at 17:44:02 GMT

Last revised: April 29, 2012 at 22:49:51 GMT


-pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/access.html b/docs/manual/access.html index d4b38c2..dcb072d 100644 --- a/docs/manual/access.html +++ b/docs/manual/access.html @@ -4,15 +4,15 @@ Accessing document data - - + +
-pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -35,11 +35,13 @@
Getting node data
Getting attribute data
Contents-based traversal functions
+
Range-based for-loop support
Traversing node/attribute lists via iterators
Recursive traversal with xml_tree_walker
Searching for nodes/attributes with predicates
+
Working with text contents
Miscellaneous functions

@@ -167,10 +169,11 @@ In this case, <description> node does not have a value, but instead has a child of type node_pcdata with value "This is a node". pugixml - provides two helper functions to parse such data: + provides several helper functions to parse such data:

const char_t* xml_node::child_value() const;
 const char_t* xml_node::child_value(const char_t* name) const;
+xml_text xml_node::text() const;
 

child_value() @@ -181,6 +184,12 @@ child with relevant type, or if the handle is null, child_value functions return empty string.

+

+ text() + returns a special object that can be used for working with PCDATA contents + in more complex cases than just retrieving the value; it is described in + Working with text contents sections. +

There is an example of using some of these functions at the end of the next section. @@ -201,26 +210,40 @@ In case the attribute handle is null, both functions return empty strings - they never return null pointers.

+

+ If you need a non-empty string if the attribute handle is null (for example, + you need to get the option value from XML attribute, but if it is not specified, + you need it to default to "sorted" + instead of ""), you + can use as_string accessor: +

+
const char_t* xml_attribute::as_string(const char_t* def = "") const;
+
+

+ It returns def argument if + the attribute handle is null. If you do not specify the argument, the function + is equivalent to value(). +

In many cases attribute values have types that are not strings - i.e. an attribute may always contain values that should be treated as integers, despite the fact that they are represented as strings in XML. pugixml provides several accessors that convert attribute value to some other type:

-
int xml_attribute::as_int() const;
-unsigned int xml_attribute::as_uint() const;
-double xml_attribute::as_double() const;
-float xml_attribute::as_float() const;
-bool xml_attribute::as_bool() const;
+
int xml_attribute::as_int(int def = 0) const;
+unsigned int xml_attribute::as_uint(unsigned int def = 0) const;
+double xml_attribute::as_double(double def = 0) const;
+float xml_attribute::as_float(float def = 0) const;
+bool xml_attribute::as_bool(bool def = false) const;
 

as_int, as_uint, as_double and as_float convert attribute values to numbers. - If attribute handle is null or attribute value is empty, 0 - is returned. Otherwise, all leading whitespace characters are truncated, - and the remaining string is parsed as a decimal number (as_int - or as_uint) or as a floating - point number in either decimal or scientific form (as_double + If attribute handle is null or attribute value is empty, def + argument is returned (which is 0 by default). Otherwise, all leading whitespace + characters are truncated, and the remaining string is parsed as a decimal + number (as_int or as_uint) or as a floating point number + in either decimal or scientific form (as_double or as_float). Any extra characters are silently discarded, i.e. as_int will return 1 for string "1abc". @@ -241,10 +264,11 @@

as_bool converts attribute - value to boolean as follows: if attribute handle is null or attribute value - is empty, false is returned. - Otherwise, true is returned - if the first character is one of '1', 't', + value to boolean as follows: if attribute handle is null, def + argument is returned (which is false + by default). If attribute value is empty, false + is returned. Otherwise, true + is returned if the first character is one of '1', 't', 'T', 'y', 'Y'. This means that strings like "true" and "yes" are recognized @@ -347,6 +371,56 @@

+

+ 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 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): +

+

+ +

+
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;
+}
+
+

+

+
+
+

+ It is common to store data as text contents of some node - i.e. <node><description>This is a node</description></node>. + In this case, <description> node does not have a value, but instead + has a child of type node_pcdata with value + "This is a node". pugixml + provides a special class, xml_text, + to work with such data. Working with text objects to modify data is described + in the documentation for modifying document + data; this section describes the access interface of xml_text. +

+

+ You can get the text object from a node by using text() method: +

+
xml_text xml_node::text() const;
+
+

+ If the node has a type node_pcdata + or node_cdata, then the node + itself is used to return data; otherwise, a first child node of type node_pcdata or node_cdata + is used. +

+

+ You can check if the text object is bound to a valid PCDATA/CDATA node by + using it as a boolean value, i.e. if + (text) { ... + } or if + (!text) { ... + }. Alternatively you can check it + by using the empty() + method: +

+
bool xml_text::empty() const;
+
+

+ Given a text object, you can get the contents (i.e. the value of PCDATA/CDATA + node) by using the following function: +

+
const char_t* xml_text::get() const;
+
+

+ In case text object is empty, the function returns an empty string - it never + returns a null pointer. +

+

+ If you need a non-empty string if the text object is empty, or if the text + contents is actually a number or a boolean that is stored as a string, you + can use the following accessors: +

+
const char_t* xml_text::as_string(const char_t* def = "") const;
+int xml_text::as_int(int def = 0) const;
+unsigned int xml_text::as_uint(unsigned int def = 0) const;
+double xml_text::as_double(double def = 0) const;
+float xml_text::as_float(float def = 0) const;
+bool xml_text::as_bool(bool def = false) const;
+
+

+ All of the above functions have the same semantics as similar xml_attribute members: they return the + default argument if the text object is empty, they convert the text contents + to a target type using the same rules and restrictions. You can refer + to documentation for the attribute functions for details. +

+

+ xml_text is essentially a + helper class that operates on xml_node + values. It is bound to a node of type node_pcdata + or [node_cdata]. You can use the following function to retrieve this node: +

+
xml_node xml_text::data() const;
+
+

+ Essentially, assuming text + is an xml_text object, callling + text.get() is + equivalent to calling text.data().value(). +

+

+ This is an example of using xml_text + object (samples/text.cpp): +

+

+ +

+
std::cout << "Project name: " << project.child("name").text().get() << std::endl;
+std::cout << "Project version: " << project.child("version").text().as_double() << std::endl;
+std::cout << "Project visibility: " << (project.child("public").text().as_bool(/* def= */ true) ? "public" : "private") << std::endl;
+std::cout << "Project description: " << project.child("description").text().get() << std::endl;
+
+

+

+
+
+

@@ -698,7 +866,7 @@

- @@ -706,7 +874,7 @@
-pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/apiref.html b/docs/manual/apiref.html index 5737c51..cf1a137 100644 --- a/docs/manual/apiref.html +++ b/docs/manual/apiref.html @@ -4,15 +4,15 @@ API Reference - - + +
-pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -60,6 +60,18 @@
  • #define PUGIXML_FUNCTION
  • +
  • + #define PUGIXML_MEMORY_PAGE_SIZE +
  • +
  • + #define PUGIXML_MEMORY_OUTPUT_STACK +
  • +
  • + #define PUGIXML_MEMORY_XPATH_PAGE_SIZE +
  • +
  • + #define PUGIXML_HEADER_ONLY +
  • Types: @@ -199,7 +211,10 @@ encoding_utf32

  • - encoding_wchar

    + encoding_wchar +
  • +
  • + encoding_latin1

  • @@ -241,9 +256,15 @@
  • format_no_declaration
  • +
  • + format_no_escapes +
  • format_raw
  • +
  • + format_save_file_text +
  • format_write_bom

    @@ -286,6 +307,9 @@
  • parse_ws_pcdata
  • +
  • + parse_ws_pcdata_single +
  • parse_wconv_attribute
  • @@ -364,20 +388,38 @@
  • - int as_int() const; + const char_t* as_string(const char_t* + def = + "") + const; +
  • +
  • + int as_int(int def = + 0) + const;
  • unsigned int - as_uint() const; + as_uint(unsigned + int def + = 0) const;
  • - double as_double() const; + double as_double(double + def = + 0) + const;
  • - float as_float() const; + float as_float(float def = + 0) + const;
  • - bool as_bool() const;

    + bool as_bool(bool def = + false) + const; +

  • @@ -517,6 +559,18 @@
  • xml_attribute last_attribute() const;

    +
  • +
  • + implementation-defined type children() const; +
  • +
  • + implementation-defined type children(const char_t* + name) + const; +
  • +
  • + implementation-defined type attributes() const;

    +
  • xml_node child(const char_t* @@ -557,7 +611,9 @@ const char_t* child_value(const char_t* name) const; -

    +
  • +
  • + xml_text text() const;

  • @@ -1023,6 +1079,108 @@
  • +
  • + class xml_text +
      +
    • + bool empty() const; +
    • +
    • + operator xml_text::unspecified_bool_type() const;

      + +
    • +
    • + const char_t* xml_text::get() const;

      + +
    • +
    • + const char_t* as_string(const char_t* + def = + "") + const; +
    • +
    • + int as_int(int def = + 0) + const; +
    • +
    • + unsigned int + as_uint(unsigned + int def + = 0) const; +
    • +
    • + double as_double(double + def = + 0) + const; +
    • +
    • + float as_float(float def = + 0) + const; +
    • +
    • + bool as_bool(bool def = + false) + const; +

      + +
    • +
    • + bool set(const char_t* + rhs); +

      + +
    • +
    • + bool set(int rhs); +
    • +
    • + bool set(unsigned + int rhs); +
    • +
    • + bool set(double + rhs); +
    • +
    • + bool set(bool rhs); +

      + +
    • +
    • + xml_text& + operator=(const char_t* + rhs); +
    • +
    • + xml_text& + operator=(int rhs); +
    • +
    • + xml_text& + operator=(unsigned + int rhs); +
    • +
    • + xml_text& + operator=(double + rhs); +
    • +
    • + xml_text& + operator=(bool rhs); +

      + +
    • +
    • + xml_node data() const;

      + +
    • +
    +
  • class xml_writer
    • @@ -1371,7 +1529,7 @@
    - @@ -1379,7 +1537,7 @@
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/changes.html b/docs/manual/changes.html index 78cde23..d119532 100644 --- a/docs/manual/changes.html +++ b/docs/manual/changes.html @@ -4,15 +4,15 @@ Changelog - - + +
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -30,6 +30,100 @@ +
    + 1.05.2012 - version + 1.2 +
    +

    + Major release, featuring header-only mode, various interface enhancements (i.e. + PCDATA manipulation and C++11 iteration), many other features and compatibility + improvements. +

    +
      +
    • + New features: +
        +
      1. + Added xml_text helper class for working with PCDATA/CDATA contents + of an element node +
      2. +
      3. + Added optional header-only mode (controlled by PUGIXML_HEADER_ONLY + define) +
      4. +
      5. + Added xml_node::children() and xml_node::attributes() for C++11 ranged + for loop or BOOST_FOREACH +
      6. +
      7. + Added support for Latin-1 (ISO-8859-1) encoding conversion during + loading and saving +
      8. +
      9. + Added custom default values for xml_attribute::as_* (they are returned if the attribute + does not exist) +
      10. +
      11. + Added parse_ws_pcdata_single flag for preserving whitespace-only + PCDATA in case it's the only child +
      12. +
      13. + Added format_save_file_text for xml_document::save_file to open files + as text instead of binary (changes newlines on Windows) +
      14. +
      15. + Added format_no_escapes flag to disable special symbol escaping (complements + ~parse_escapes) +
      16. +
      17. + Added support for loading document from streams that do not support + seeking +
      18. +
      19. + Added PUGIXML_MEMORY_* constants for tweaking allocation behavior (useful for embedded + systems) +
      20. +
      21. + Added PUGIXML_VERSION preprocessor define +
      22. +
      +
    • +
    • + Compatibility improvements: +
        +
      1. + Parser does not require setjmp support (improves compatibility with + some embedded platforms, enables clr:pure compilation) +
      2. +
      3. + STL forward declarations are no longer used (fixes SunCC/RWSTL compilation, + fixes clang compilation in C++11 mode) +
      4. +
      5. + Fixed AirPlay SDK, Android, Windows Mobile (WinCE) and C++/CLI compilation +
      6. +
      7. + Fixed several compilation warnings for various GCC versions, Intel + C++ compiler and Clang +
      8. +
      +
    • +
    • + Bug fixes: +
        +
      1. + Fixed unsafe bool conversion to avoid problems on C++/CLI +
      2. +
      3. + Iterator dereference operator is const now (fixes Boost filter_iterator + support) +
      4. +
      5. + xml_document::save_file now checks for file I/O errors during saving +
      6. +
      +
    • +
    1.11.2010 - version 1.0 @@ -760,7 +854,7 @@ - @@ -768,7 +862,7 @@
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/dom.html b/docs/manual/dom.html index 22509a9..22d8d83 100644 --- a/docs/manual/dom.html +++ b/docs/manual/dom.html @@ -4,15 +4,15 @@ Document object model - - + +
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -40,6 +40,7 @@
    Custom memory allocation/deallocation functions
    +
    Memory consumption tuning
    Document memory management internals
    @@ -102,16 +103,17 @@
    • Plain character data nodes (node_pcdata) represent plain text in XML. PCDATA nodes have a value, but do not have - a name or children/attributes. Note that plain character data is not - a part of the element node but instead has its own node; for example, - an element node can have several child PCDATA nodes. The example XML - representation of text nodes is as follows: + a name or children/attributes. Note that plain + character data is not a part of the element node but instead has its + own node; an element node can have several child PCDATA nodes. + The example XML representation of text nodes is as follows:
    <node> text1 <child/> text2 </node>
     

    Here "node" element - has three children, two of which are PCDATA nodes with values "text1" and "text2". + has three children, two of which are PCDATA nodes with values " text1 " and " + text2 ".

    • Character data nodes (node_cdata) represent @@ -617,6 +619,54 @@
    +

    + There are several important buffering optimizations in pugixml that rely + on predefined constants. These constants have default values that were + tuned for common usage patterns; for some applications, changing these + constants might improve memory consumption or increase performance. Changing + these constants is not recommended unless their default values result in + visible problems. +

    +

    + These constants can be tuned via configuration defines, as discussed in + Additional configuration + options; it is recommended to set them in pugiconfig.hpp. +

    +
      +
    • + PUGIXML_MEMORY_PAGE_SIZE + controls the page size for document memory allocation. Memory for node/attribute + objects is allocated in pages of the specified size. The default size + is 32 Kb; for some applications the size is too large (i.e. embedded + systems with little heap space or applications that keep lots of XML + documents in memory). A minimum size of 1 Kb is recommended.

      + +
    • +
    • + PUGIXML_MEMORY_OUTPUT_STACK + controls the cumulative stack space required to output the node. Any + output operation (i.e. saving a subtree to file) uses an internal buffering + scheme for performance reasons. The default size is 10 Kb; if you're + using node output from threads with little stack space, decreasing + this value can prevent stack overflows. A minimum size of 1 Kb is recommended. +

      + +
    • +
    • + PUGIXML_MEMORY_XPATH_PAGE_SIZE + controls the page size for XPath memory allocation. Memory for XPath + query objects as well as internal memory for XPath evaluation is allocated + in pages of the specified size. The default size is 4 Kb; if you have + a lot of resident XPath query objects, you might need to decrease the + size to improve memory consumption. A minimum size of 256 bytes is + recommended. +
    • +
    +
    + - @@ -665,7 +715,7 @@
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/install.html b/docs/manual/install.html index 9809a39..df7b322 100644 --- a/docs/manual/install.html +++ b/docs/manual/install.html @@ -4,15 +4,15 @@ Installation - - - + + +
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -44,6 +44,8 @@ a standalone static library
    Building pugixml as a standalone shared library
    +
    Using pugixml in header-only + mode
    Additional configuration options
    @@ -65,8 +67,8 @@ You can download the latest source distribution via one of the following links:

    -
    http://pugixml.googlecode.com/files/pugixml-1.0.zip
    -http://pugixml.googlecode.com/files/pugixml-1.0.tar.gz
    +
    http://pugixml.googlecode.com/files/pugixml-1.2.zip
    +http://pugixml.googlecode.com/files/pugixml-1.2.tar.gz
     

    The distribution contains library source, documentation (the manual you're @@ -94,7 +96,7 @@

    For example, to checkout the current version, you can use this command:

    -
    svn checkout http://pugixml.googlecode.com/svn/tags/release-1.0 pugixml
    +
    svn checkout http://pugixml.googlecode.com/svn/tags/release-1.2 pugixml

    To checkout the latest version, you can use this command:

    @@ -269,6 +271,58 @@

    +
    + +

    + It's possible to use pugixml in header-only mode. This means that all source + code for pugixml will be included in every translation unit that includes + pugixml.hpp. This is how most of Boost and STL libraries work. +

    +

    + Note that there are advantages and drawbacks of this approach. Header mode + may improve tree traversal/modification performance (because many simple + functions will be inlined), if your compiler toolchain does not support + link-time optimization, or if you have it turned off (with link-time optimization + the performance should be similar to non-header mode). However, since compiler + now has to compile pugixml source once for each translation unit that includes + it, compilation times may increase noticeably. If you want to use pugixml + in header mode but do not need XPath support, you can consider disabling + it by using PUGIXML_NO_XPATH define + to improve compilation time. +

    +

    + Enabling header-only mode is a two-step process: +

    +
      +
    1. + You have to define PUGIXML_HEADER_ONLY +
    2. +
    3. + You have to include pugixml.cpp whenever you include pugixml.hpp +
    4. +
    +

    + Both of these are best done via pugiconfig.hpp like this: +

    +
    #define PUGIXML_HEADER_ONLY
    +#include "pugixml.cpp"
    +
    +

    + Note that it is safe to compile pugixml.cpp if PUGIXML_HEADER_ONLY + is defined - so if you want to i.e. use header-only mode only in Release + configuration, you can include pugixml.cpp in your project (see Building pugixml as + a part of another static library/executable), + and conditionally enable header-only mode in pugiconfig.hpp, i.e.: +

    +
    #ifndef _DEBUG
    +    #define PUGIXML_HEADER_ONLY
    +    #include "pugixml.cpp"
    +#endif
    +
    +
    +

    + PUGIXML_MEMORY_PAGE_SIZE, PUGIXML_MEMORY_OUTPUT_STACK + and PUGIXML_MEMORY_XPATH_PAGE_SIZE + can be used to customize certain important sizes to optimize memory usage + for the application-specific patterns. For details see Memory consumption tuning. +

    @@ -367,7 +427,8 @@
  • Microsoft Visual C++ 6.0, 7.0 (2002), 7.1 (2003), 8.0 (2005) x86/x64, - 9.0 (2008) x86/x64, 10.0 (2010) x86/x64 + 9.0 (2008) x86/x64, 10.0 (2010) x86/x64, 11.0 x86/x64/ARM and some + CLR versions
  • MinGW (GCC) 3.4, 4.4, 4.5, 4.6 x64 @@ -383,6 +444,9 @@
  • Apple MacOSX (GCC 4.0.1 x86/x64/PowerPC)
  • +
  • + Sun Solaris (sunCC x86/x64) +
  • Microsoft Xbox 360
  • @@ -395,6 +459,10 @@
  • Sony Playstation 3 (GCC 4.1.1, SNC 310.1)
  • +
  • + Various portable platforms (Android NDK, BlackBerry NDK, Samsung bada, + Windows CE) +
  • @@ -405,7 +473,7 @@
    - @@ -413,7 +481,7 @@
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/loading.html b/docs/manual/loading.html index 5b5576b..a26b62c 100644 --- a/docs/manual/loading.html +++ b/docs/manual/loading.html @@ -4,15 +4,15 @@ Loading document - - + +
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -282,10 +282,6 @@

    -

    - Stream loading requires working seek/tell functions and therefore may fail - when used with some stream implementations like gzstream. -

    @@ -384,7 +380,9 @@ returned by description() function may change from version to version, so any complex status handling should be based on status - value. + value. Note that description() returns a char + string even in PUGIXML_WCHAR_MODE; + you'll have to call as_wide to get the wchar_t string.

    If parsing failed because the source data was not a valid XML, the resulting @@ -533,6 +531,25 @@ " "), and only one child when parse_ws_pcdata is not set. This flag is off by default. +

    + + +

  • + parse_ws_pcdata_single determines + if whitespace-only PCDATA nodes that have no sibling nodes are to be + put in DOM tree. In some cases application needs to parse the whitespace-only + contents of nodes, i.e. <node> + </node>, but is not interested in whitespace + markup elsewhere. It is possible to use parse_ws_pcdata + flag in this case, but it results in excessive allocations and complicates + document processing in some cases; this flag is intended to avoid that. + As an example, after parsing XML string <node> + <a> </a> </node> with parse_ws_pcdata_single + flag set, <node> element will have one child <a>, and <a> + element will have one child with type node_pcdata + and value " ". + This flag has no effect if parse_ws_pcdata + is enabled. This flag is off by default.
  • @@ -581,8 +598,7 @@ attributes. This means, that after attribute values are normalized as if parse_wconv_attribute was set, leading and trailing space characters are removed, and all sequences - of space characters are replaced by a single space character. The value - of parse_wconv_attribute + of space characters are replaced by a single space character. parse_wconv_attribute has no effect if this flag is on. This flag is off by default. @@ -755,6 +771,10 @@ or encoding_utf32, depending on wchar_t size. +

  • + encoding_latin1 corresponds to ISO-8859-1 + encoding (also known as Latin-1). +
  • The algorithm used for encoding_auto @@ -828,7 +848,7 @@

    - @@ -836,7 +856,7 @@
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/modify.html b/docs/manual/modify.html index 3db02e1..b039dc7 100644 --- a/docs/manual/modify.html +++ b/docs/manual/modify.html @@ -4,15 +4,15 @@ Modifying document data - - + +
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -35,6 +35,7 @@
    Setting attribute data
    Adding nodes/attributes
    Removing nodes/attributes
    +
    Working with text contents
    Cloning nodes/attributes

    @@ -406,6 +407,85 @@

    +

    + pugixml provides a special class, xml_text, + to work with text contents stored as a value of some node, i.e. <node><description>This is a node</description></node>. + Working with text objects to retrieve data is described in the + documentation for accessing document data; this section describes + the modification interface of xml_text. +

    +

    + Once you have an xml_text + object, you can set the text contents using the following function: +

    +
    bool xml_text::set(const char_t* rhs);
    +
    +

    + This function tries to set the contents to the specified string, and returns + the operation result. The operation fails if the text object was retrieved + from a node that can not have a value and that is not an element node (i.e. + it is a node_declaration node), if + the text object is empty, or if there is insufficient memory to handle the + request. The provided string is copied into document managed memory and can + be destroyed after the function returns (for example, you can safely pass + stack-allocated buffers to this function). Note that if the text object was + retrieved from an element node, this function creates the PCDATA child node + if necessary (i.e. if the element node does not have a PCDATA/CDATA child + already). +

    +

    + In addition to a string function, several functions are provided for handling + text with numbers and booleans as contents: +

    +
    bool xml_text::set(int rhs);
    +bool xml_text::set(unsigned int rhs);
    +bool xml_text::set(double rhs);
    +bool xml_text::set(bool rhs);
    +
    +

    + The above functions convert the argument to string and then call the base + set function. These functions + have the same semantics as similar xml_attribute + functions. You can refer to documentation + for the attribute functions for details. +

    +

    + For convenience, all set + functions have the corresponding assignment operators: +

    +
    xml_text& xml_text::operator=(const char_t* rhs);
    +xml_text& xml_text::operator=(int rhs);
    +xml_text& xml_text::operator=(unsigned int rhs);
    +xml_text& xml_text::operator=(double rhs);
    +xml_text& xml_text::operator=(bool rhs);
    +
    +

    + These operators simply call the right set + function and return the attribute they're called on; the return value of + set is ignored, so errors + are ignored. +

    +

    + This is an example of using xml_text + object to modify text contents (samples/text.cpp): +

    +

    + +

    +
    // change project version
    +project.child("version").text() = 1.2;
    +
    +// add description element and set the contents
    +// note that we do not have to explicitly add the node_pcdata child
    +project.append_child("description").text().set("a test project");
    +
    +

    +

    +
    +
    +

    @@ -528,7 +608,7 @@

    - @@ -536,7 +616,7 @@
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/saving.html b/docs/manual/saving.html index abaf9f2..2be70cb 100644 --- a/docs/manual/saving.html +++ b/docs/manual/saving.html @@ -4,15 +4,15 @@ Saving document - - + +
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -37,6 +37,7 @@
    Saving a single subtree
    Output options
    Encodings
    +
    Customizing document declaration

    Often after creating a new document or loading the existing one and processing @@ -52,8 +53,9 @@

    Before writing to the destination the node/attribute data is properly formatted according to the node type; all special XML symbols, such as < and &, - are properly escaped. In order to guard against forgotten node/attribute names, - empty node/attribute names are printed as ":anonymous". + are properly escaped (unless format_no_escapes + flag is set). In order to guard against forgotten node/attribute names, empty + node/attribute names are printed as ":anonymous". For well-formed output, make sure all node and attribute names are set to meaningful values.

    @@ -179,11 +181,11 @@

    In order to output the document via some custom transport, for example sockets, - you should create an object which implements xml_writer_file + you should create an object which implements xml_writer interface and pass it to save - function. xml_writer_file::write - function is called with a buffer as an input, where data - points to buffer start, and size + function. xml_writer::write function is called with a buffer + as an input, where data points + to buffer start, and size is equal to the buffer size in bytes. write implementation must write the buffer to the transport; it can not save the passed buffer pointer, as the buffer contents will change after write returns. The buffer contains the @@ -192,9 +194,8 @@

    write function is called with relatively large blocks (size is usually several kilobytes, except for - the first block with BOM, which is output only if format_write_bom - is set, and last block, which may be small), so there is often no need for - additional buffering in the implementation. + the last block that may be small), so there is often no need for additional + buffering in the implementation.

    This is a simple example of custom writer for saving document data to STL @@ -310,7 +311,19 @@ to be read by humans; also it can be useful if the document was parsed with parse_ws_pcdata flag, to preserve the original document formatting as much as possible. This flag - is off by default. + is off by default.

    + + +

  • + format_no_escapes disables output + escaping for attribute values and PCDATA contents. If this flag is on, + special symbols (', &, <, >) and all non-printable characters + (those with codepoint values less than 32) are converted to XML escape + sequences (i.e. &amp;) during output. If this flag is off, no text + processing is performed; therefore, output XML can be malformed if output + contents contains invalid symbols (i.e. having a stray < in the PCDATA + will make the output malformed). This flag is on + by default.
  • @@ -337,6 +350,16 @@ functions: they never output the BOM. This flag is off by default. +

  • + format_save_file_text changes + the file mode when using save_file + function. By default, file is opened in binary mode, which means that + the output file will contain platform-independent newline \n (ASCII 10). + If this flag is on, file is opened in text mode, which on some systems + changes the newline format (i.e. on Windows you can use this flag to + output XML documents with \r\n (ASCII 13 10) newlines. This flag is + off by default. +
  • Additionally, there is one predefined option mask: @@ -435,10 +458,67 @@

    +
    + +

    + When you are saving the document using xml_document::save() or xml_document::save_file(), a default XML document declaration is + output, if format_no_declaration + is not speficied and if the document does not have a declaration node. However, + the default declaration is not customizable. If you want to customize the + declaration output, you need to create the declaration node yourself. +

    +
    + + + + + +
    [Note]Note

    + By default the declaration node is not added to the document during parsing. + If you just need to preserve the original declaration node, you have to + add the flag parse_declaration + to the parsing flags; the resulting document will contain the original + declaration node, which will be output during saving. +

    +

    + Declaration node is a node with type node_declaration; + it behaves like an element node in that it has attributes with values (but + it does not have child nodes). Therefore setting custom version, encoding + or standalone declaration involves adding attributes and setting attribute + values. +

    +

    + This is an example that shows how to create a custom declaration node (samples/save_declaration.cpp): +

    +

    + +

    +
    // get a test document
    +pugi::xml_document doc;
    +doc.load("<foo bar='baz'><call>hey</call></foo>");
    +
    +// add a custom declaration node
    +pugi::xml_node decl = doc.prepend_child(pugi::node_declaration);
    +decl.append_attribute("version") = "1.0";
    +decl.append_attribute("encoding") = "UTF-8";
    +decl.append_attribute("standalone") = "no";
    +
    +// <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    +// <foo bar="baz">
    +//         <call>hey</call>
    +// </foo>
    +doc.save(std::cout);
    +std::cout << std::endl;
    +
    +

    +

    +
    - @@ -446,7 +526,7 @@
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/toc.html b/docs/manual/toc.html index 97d0b6c..d9fe5f8 100644 --- a/docs/manual/toc.html +++ b/docs/manual/toc.html @@ -4,14 +4,14 @@ Table of Contents - - + +
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -52,6 +52,8 @@ a standalone static library
    Building pugixml as a standalone shared library
    +
    Using pugixml in header-only + mode
    Additional configuration options
    @@ -68,6 +70,7 @@
    Custom memory allocation/deallocation functions
    +
    Memory consumption tuning
    Document memory management internals
    @@ -88,11 +91,13 @@
    Getting node data
    Getting attribute data
    Contents-based traversal functions
    +
    Range-based for-loop support
    Traversing node/attribute lists via iterators
    Recursive traversal with xml_tree_walker
    Searching for nodes/attributes with predicates
    +
    Working with text contents
    Miscellaneous functions
    Modifying document data
    @@ -101,6 +106,7 @@
    Setting attribute data
    Adding nodes/attributes
    Removing nodes/attributes
    +
    Working with text contents
    Cloning nodes/attributes
    Saving document
    @@ -111,6 +117,7 @@
    Saving a single subtree
    Output options
    Encodings
    +
    Customizing document declaration
    XPath
    @@ -128,7 +135,7 @@ - @@ -136,7 +143,7 @@
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/manual/xpath.html b/docs/manual/xpath.html index ea6b956..bb37f64 100644 --- a/docs/manual/xpath.html +++ b/docs/manual/xpath.html @@ -4,15 +4,15 @@ XPath - - + +
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: @@ -624,7 +624,11 @@

    description() member function can be used to get the error message; it never returns the - null pointer, so you can safely use description() even if query parsing succeeded. + null pointer, so you can safely use description() even if query parsing succeeded. Note that + description() + returns a char string even in + PUGIXML_WCHAR_MODE; you'll + have to call as_wide to get the wchar_t string.

    In addition to the error message, parsing result has an offset @@ -717,7 +721,7 @@ - @@ -725,7 +729,7 @@
    -pugixml 1.0 manual | +pugixml 1.2 manual | Overview | Installation | Document: diff --git a/docs/quickstart.html b/docs/quickstart.html index d39e552..75a2b1e 100644 --- a/docs/quickstart.html +++ b/docs/quickstart.html @@ -1,34 +1,5 @@ - - - -pugixml 1.0 - - - - - -
    -
    - - -
    - -

    - pugixml is a light-weight C++ XML +pugixml 1.2

    + pugixml is a light-weight C++ XML processing library. It consists of a DOM-like interface with rich traversal/modification capabilities, an extremely fast XML parser which constructs the DOM tree from an XML file/buffer, and an XPath 1.0 implementation for complex data-driven @@ -39,54 +10,36 @@ and has many users. All code is distributed under the MIT license, making it completely free to use in both open-source and proprietary applications. -

    -

    +

    pugixml enables very fast, convenient and memory-efficient XML document processing. However, since pugixml has a DOM parser, it can't process XML documents that do not fit in memory; also the parser is a non-validating one, so if you need DTD/Schema validation, the library is not for you. -

    -

    +

    This is the quick start guide for pugixml, which purpose is to enable you to start using the library quickly. Many important library features are either not described at all or only mentioned briefly; for more complete information - you should read the complete manual. -

    -
    - - - - -
    [Note]Note

    + you should read the complete manual. +

    -
    [Note]Note

    No documentation is perfect, neither is this one. If you encounter a description that is unclear, please file an issue as described in Feedback. Also if you can spare the time for a full proof-reading, including spelling and grammar, that would be great! Please send me an e-mail; as a token of appreciation, your name will be included into the corresponding section of the manual. -

    - -
    - -

    +

    pugixml is distributed in source form. You can download a source distribution via one of the following links: -

    -
    http://pugixml.googlecode.com/files/pugixml-1.0.zip
    -http://pugixml.googlecode.com/files/pugixml-1.0.tar.gz
    -
    -

    +

    http://pugixml.googlecode.com/files/pugixml-1.2.zip
    +http://pugixml.googlecode.com/files/pugixml-1.2.tar.gz
    +

    The distribution contains library source, documentation (the guide you're reading now and the manual) and some code examples. After downloading the distribution, install pugixml by extracting all files from the compressed archive. The files have different line endings depending on the archive format - .zip archive has Windows line endings, .tar.gz archive has Unix line endings. Otherwise the files in both archives are identical. -

    -

    +

    The complete pugixml source consists of three files - one source file, pugixml.cpp, and two header files, pugixml.hpp and pugiconfig.hpp. pugixml.hpp is the primary header which you need to include in order to use pugixml classes/functions. @@ -95,23 +48,16 @@ can find the header; however you can also use relative path (i.e. #include "../libs/pugixml/src/pugixml.hpp") or include directory-relative path (i.e. #include <xml/thirdparty/pugixml/src/pugixml.hpp>). -

    -

    +

    The easiest way to build pugixml is to compile the source file, pugixml.cpp, along with the existing library/executable. This process depends on the method of building your application; for example, if you're using Microsoft Visual Studio[1], Apple Xcode, Code::Blocks or any other IDE, just add pugixml.cpp to one of your projects. There are other building methods available, including building - pugixml as a standalone static/shared library; read + pugixml as a standalone static/shared library; read the manual for further information. -

    -
    -

    pugixml stores XML data in DOM-like way: the entire XML document (both document structure and element data) is stored in memory as a tree. The tree can be loaded from character stream (file, string, C++ I/O stream), then traversed @@ -119,8 +65,7 @@ structure and node/attribute data can be changed at any time. Finally, the result of document transformations can be saved to a character stream (file, C++ I/O stream or custom transport). -

    -

    +

    The root of the tree is the document itself, which corresponds to C++ type xml_document. Document has one or more child nodes, which correspond to C++ type xml_node. @@ -128,55 +73,40 @@ of child nodes, a collection of attributes, which correspond to C++ type xml_attribute, and some additional data (i.e. name). -

    -

    +

    The most common node types are: -

    -
      -
    • +

      • Document node (node_document) - this is the root of the tree, which consists of several child nodes. This node corresponds to xml_document class; note that xml_document is a sub-class of xml_node, so the entire node interface is also available. -
      • -
      • +
      • Element/tag node (node_element) - this is the most common type of node, which represents XML elements. Element nodes have a name, a collection of attributes and a collection of child nodes (both of which may be empty). The attribute is a simple name/value pair. -
      • -
      • +
      • Plain character data nodes (node_pcdata) represent plain text in XML. PCDATA nodes have a value, but do not have - name or children/attributes. Note that plain character data is not a - part of the element node but instead has its own node; for example, an - element node can have several child PCDATA nodes. -
      • -
      -

      + name or children/attributes. Note that plain character + data is not a part of the element node but instead has its own node; + for example, an element node can have several child PCDATA nodes. +

    Despite the fact that there are several node types, there are only three C++ types representing the tree (xml_document, xml_node, xml_attribute); some operations on xml_node are only valid for certain node types. They are described below. -

    -
    - - - - -
    [Note]Note

    +

    -
    [Note]Note

    All pugixml classes and functions are located in pugi namespace; you have to either use explicit name qualification (i.e. pugi::xml_node), or to gain access to relevant symbols via using directive (i.e. using pugi::xml_node; or using namespace pugi;). -

    -

    +

    xml_document is the owner of the entire document structure; destroying the document destroys the whole tree. The interface of xml_document @@ -184,16 +114,14 @@ of xml_node, which allows for document inspection and/or modification. Note that while xml_document is a sub-class of xml_node, xml_node is not a polymorphic type; the inheritance is present only to simplify usage. -

    -

    +

    xml_node is the handle to document node; it can point to any node in the document, including document itself. There is a common interface for nodes of all types. Note that xml_node is only a handle to the actual node, not the node itself - you can have several xml_node handles pointing to the same underlying object. Destroying xml_node handle does not destroy the node and does not remove it from the tree. -

    -

    +

    There is a special value of xml_node type, known as null node or empty node. It does not correspond to any node in any document, and thus resembles null pointer. However, all operations @@ -206,29 +134,21 @@ don't have to check for errors twice. You can test if a handle is null via implicit boolean cast: if (node) { ... } or if (!node) { ... }. -

    -

    +

    xml_attribute is the handle to an XML attribute; it has the same semantics as xml_node, i.e. there can be several xml_attribute handles pointing to the same underlying object and there is a special null attribute value, which propagates to function results. -

    -

    +

    There are two choices of interface and internal representation when configuring pugixml: you can either choose the UTF-8 (also called char) interface or UTF-16/32 (also called wchar_t) one. The choice is controlled via PUGIXML_WCHAR_MODE define; you can set it via pugiconfig.hpp or via preprocessor options. All tree functions that work with strings work with either C-style null terminated strings or STL - strings of the selected character type. Read + strings of the selected character type. Read the manual for additional information on Unicode interface. -

    -
    -

    pugixml provides several functions for loading XML data from various places - files, C++ iostreams, memory buffers. All functions use an extremely fast non-validating parser. This parser is not fully W3C conformant - it can load @@ -239,36 +159,29 @@ Unicode encodings (UTF-8, UTF-16 (big and little endian), UTF-32 (big and little endian); UCS-2 is naturally supported since it's a strict subset of UTF-16) and handles all encoding conversions automatically. -

    -

    +

    The most common source of XML data is files; pugixml provides a separate function for loading XML document from file. This function accepts file path as its first argument, and also two optional arguments, which specify parsing options and input data encoding, which are described in the manual. -

    -

    - This is an example of loading XML document from file (samples/load_file.cpp): -

    -

    +

    + This is an example of loading XML document from file (samples/load_file.cpp): +

    -

    -
    pugi::xml_document doc;
    +

    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;
    -
    -

    -

    -

    +

    +

    load_file, as well as other loading functions, destroys the existing document tree and then tries to load the new tree from the specified file. The result of the operation is returned in an xml_parse_result object; this object contains the operation status, and the related information (i.e. last successfully parsed position in the input file, if parsing fails). -

    -

    +

    Parsing result object can be implicitly converted to bool; if you do not want to handle parsing errors thoroughly, you can just check the return value of load functions as if it was a bool: @@ -277,14 +190,11 @@ Otherwise you can use the status member to get parsing status, or the description() member function to get the status in a string form. -

    -

    - This is an example of handling loading errors (samples/load_error_handling.cpp): -

    -

    +

    + This is an example of handling loading errors (samples/load_error_handling.cpp): +

    -

    -
    pugi::xml_document doc;
    +

    pugi::xml_document doc;
     pugi::xml_parse_result result = doc.load(source);
     
     if (result)
    @@ -295,10 +205,8 @@
         std::cout << "Error description: " << result.description() << "\n";
         std::cout << "Error offset: " << result.offset << " (error at [..." << (source + result.offset) << "]\n\n";
     }
    -
    -

    -

    -

    +

    +

    Sometimes XML data should be loaded from some other source than file, i.e. HTTP URL; also you may want to load XML data from file using non-standard functions, i.e. to use your virtual file system facilities or to load XML @@ -308,8 +216,7 @@ document from C++ IOstream, in which case you should provide an object which implements std::istream or std::wistream interface. -

    -

    +

    There are different functions for loading document from memory; they treat the passed buffer as either an immutable one (load_buffer), a mutable buffer which is owned by the caller (load_buffer_inplace), @@ -317,24 +224,18 @@ There is also a simple helper function, xml_document::load, for cases when you want to load the XML document from null-terminated character string. -

    -

    +

    This is an example of loading XML document from memory using one of these - functions (samples/load_memory.cpp); + functions (samples/load_memory.cpp); read the sample code for more examples: -

    -

    +

    -

    -
    const char source[] = "<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>";
    +

    const char source[] = "<mesh name='sphere'><bounds>0 0 1 1</bounds></mesh>";
     size_t size = sizeof(source);
    -
    -

    -

    -

    +

    +

    -

    -
    // You can use load_buffer_inplace to load document from mutable memory block; the block's lifetime must exceed that of document
    +

    // 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);
     
    @@ -343,95 +244,70 @@
     
     // You have to destroy the block yourself after the document is no longer used
     delete[] buffer;
    -
    -

    -

    -

    +

    +

    This is a simple example of loading XML document from file using streams - (samples/load_stream.cpp); read + (samples/load_stream.cpp); read the sample code for more complex examples involving wide streams and locales: -

    -

    +

    -

    -
    std::ifstream stream("weekly-utf-8.xml");
    +

    std::ifstream stream("weekly-utf-8.xml");
     pugi::xml_parse_result result = doc.load(stream);
    -
    -

    -

    -
    -

    pugixml features an extensive interface for getting various types of data from the document and for traversing the document. You can use various accessors to get node/attribute data, you can traverse the child node/attribute lists via accessors or iterators, you can do depth-first traversals with xml_tree_walker objects, and you can use XPath for complex data-driven queries. -

    -

    +

    You can get node or attribute name via name() accessor, and value via value() accessor. Note that both functions never return null pointers - they either return a string with the relevant content, or an empty string if name/value is absent or if the handle is null. Also there are two notable things for reading values: -

    -
      -
    • +

      • It is common to store data as text contents of some node - i.e. <node><description>This is a node</description></node>. In this case, <description> node does not have a value, but instead has a child of type node_pcdata with value "This is a node". - pugixml provides child_value() helper functions to parse such data. -
      • -
      • + pugixml provides child_value() and text() helper functions to parse such data. +
      • In many cases attribute values have types that are not strings - i.e. an attribute may always contain values that should be treated as integers, despite the fact that they are represented as strings in XML. pugixml provides several accessors that convert attribute value to some other type. -
      • -
      -

      - This is an example of using these functions (samples/traverse_base.cpp): -

      -

      +

    + This is an example of using these functions (samples/traverse_base.cpp): +

    -

    -
    for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
    +

    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";
     }
    -
    -

    -

    -

    +

    +

    Since a lot of document traversal consists of finding the node/attribute with the correct name, there are special functions for that purpose. For example, child("Tool") returns the first node which has the name "Tool", or null handle if there is no such node. This is an example of using such - functions (samples/traverse_base.cpp): -

    -

    + functions (samples/traverse_base.cpp): +

    -

    -
    std::cout << "Tool for *.dae generation: " << tools.find_child_by_attribute("Tool", "OutputFileMasks", "*.dae").attribute("Filename").value() << "\n";
    +

    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";
     }
    -
    -

    -

    -

    +

    +

    Child node lists and attribute lists are simply double-linked lists; while you can use previous_sibling/next_sibling and other such functions for iteration, pugixml additionally provides node and attribute iterators, so @@ -440,14 +316,11 @@ iterators are invalidated if the node/attribute objects they're pointing to are removed from the tree; adding nodes/attributes does not invalidate any iterators. -

    -

    - Here is an example of using iterators for document traversal (samples/traverse_iter.cpp): -

    -

    +

    + Here is an example of using iterators for document traversal (samples/traverse_iter.cpp): +

    -

    -
    for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
    +

    for (pugi::xml_node_iterator it = tools.begin(); it != tools.end(); ++it)
     {
         std::cout << "Tool:";
     
    @@ -458,10 +331,37 @@
     
         std::cout << std::endl;
     }
    -
    -

    -

    -

    +

    +

    + 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 Boost Foreach, + and possibly other pre-C++11 foreach facilities. +

    + Here is an example of using C++11 range-based for loop for document traversal + (samples/traverse_rangefor.cpp): +

    + +

    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;
    +}
    +

    +

    The methods described above allow traversal of immediate children of some node; if you want to do a deep tree traversal, you'll have to do it via a recursive function or some equivalent method. However, pugixml provides a @@ -469,14 +369,11 @@ to implement xml_tree_walker interface and to call traverse function. -

    -

    - This is an example of traversing tree hierarchy with xml_tree_walker (samples/traverse_walker.cpp): -

    -

    +

    + This is an example of traversing tree hierarchy with xml_tree_walker (samples/traverse_walker.cpp): +

    -

    -
    struct simple_walker: pugi::xml_tree_walker
    +

    struct simple_walker: pugi::xml_tree_walker
     {
         virtual bool for_each(pugi::xml_node& node)
         {
    @@ -487,27 +384,20 @@
             return true; // continue traversal
         }
     };
    -
    -

    -

    -

    +

    +

    -

    -
    simple_walker walker;
    +

    simple_walker walker;
     doc.traverse(walker);
    -
    -

    -

    -

    +

    +

    Finally, for complex queries often a higher-level DSL is needed. pugixml provides an implementation of XPath 1.0 language for such queries. The complete description of XPath usage can be found in the manual, but here are some examples: -

    -

    +

    -

    -
    pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']");
    +

    pugi::xpath_node_set tools = doc.select_nodes("/Profile/Tools/Tool[@AllowRemote='true' and @DeriveCaptionFrom='lastparam']");
     
     std::cout << "Tools:";
     
    @@ -520,25 +410,11 @@
     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";
    -
    -

    -

    -
    - - - - -
    [Caution]Caution

    +

    +

    -
    [Caution]Caution

    XPath functions throw xpath_exception objects on error; the sample above does not catch these exceptions. -

    - -

    The document in pugixml is fully mutable: you can completely change the document structure and modify the data of nodes/attributes. All functions take care of memory management and structural integrity themselves, so they always @@ -549,27 +425,23 @@ memory you can create documents from scratch with pugixml and later save them to file/stream instead of relying on error-prone manual text writing and without too much overhead. -

    -

    +

    All member functions that change node/attribute data or structure are non-constant and thus can not be called on constant handles. However, you can easily convert constant handle to non-constant one by simple assignment: void foo(const pugi::xml_node& n) { pugi::xml_node nc = n; }, so const-correctness here mainly provides additional documentation. -

    -

    +

    As discussed before, nodes can have name and value, both of which are strings. Depending on node type, name or value may be absent. You can use set_name and set_value member functions to set them. Similar functions are available for attributes; however, the set_value function is overloaded for some other types except strings, like floating-point numbers. Also, attribute value can be set using an assignment operator. This is an - example of setting node/attribute name and value (samples/modify_base.cpp): -

    -

    + example of setting node/attribute name and value (samples/modify_base.cpp): +

    -

    -
    pugi::xml_node node = doc.child("node");
    +

    pugi::xml_node node = doc.child("node");
     
     // change node name
     std::cout << node.set_name("notnode");
    @@ -581,13 +453,10 @@
     
     // 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;
    -
    -

    -

    -

    +

    +

    -

    -
    pugi::xml_attribute attr = node.attribute("id");
    +

    pugi::xml_attribute attr = node.attribute("id");
     
     // change attribute name/value
     std::cout << attr.set_name("key") << ", " << attr.set_value("345");
    @@ -600,10 +469,8 @@
     // we can also use assignment operators for more concise code
     attr = true;
     std::cout << "final attribute value: " << attr.value() << std::endl;
    -
    -

    -

    -

    +

    +

    Nodes and attributes do not exist without a document tree, so you can't create them without adding them to some document. A node or attribute can be created at the end of node/attribute list or before/after some other node. All insertion @@ -611,26 +478,16 @@ handle on failure. Even if the operation fails (for example, if you're trying to add a child node to PCDATA node), the document remains in consistent state, but the requested node/attribute is not added. -

    -
    - - - - -
    [Caution]Caution

    +

    -
    [Caution]Caution

    attribute() and child() functions do not add attributes or nodes to the tree, so code like node.attribute("id") = 123; will not do anything if node does not have an attribute with name "id". Make sure you're operating with existing attributes/nodes by adding them if necessary. -

    -

    - This is an example of adding new attributes/nodes to the document (samples/modify_add.cpp): -

    -

    +

    + This is an example of adding new attributes/nodes to the document (samples/modify_add.cpp): +

    -

    -
    // add node with some name
    +

    // add node with some name
     pugi::xml_node node = doc.append_child("node");
     
     // add description node with text child
    @@ -644,10 +501,8 @@
     param.append_attribute("name") = "version";
     param.append_attribute("value") = 1.1;
     param.insert_attribute_after("type", param.attribute("name")) = "float";
    -
    -

    -

    -

    +

    +

    If you do not want your document to contain some node or attribute, you can remove it with remove_attribute and remove_child functions. @@ -656,14 +511,11 @@ node also invalidates all past-the-end iterators to its attribute or child node list. Be careful to ensure that all such handles and iterators either do not exist or are not used after the attribute/node is removed. -

    -

    - This is an example of removing attributes/nodes from the document (samples/modify_remove.cpp): -

    -

    +

    + This is an example of removing attributes/nodes from the document (samples/modify_remove.cpp): +

    -

    -
    // remove description node with the whole subtree
    +

    // remove description node with the whole subtree
     pugi::xml_node node = doc.child("node");
     node.remove_child("description");
     
    @@ -674,15 +526,8 @@
     // we can also remove nodes/attributes by handles
     pugi::xml_attribute id = param.attribute("name");
     param.remove_attribute(id);
    -
    -

    -

    -
    -
    - -

    +

    +

    Often after creating a new document or loading the existing one and processing it, it is necessary to save the result back to file. Also it is occasionally useful to output the whole document or a subtree to some stream; use cases @@ -691,28 +536,22 @@ the document to a file, stream or another generic transport interface; these functions allow to customize the output format, and also perform necessary encoding conversions. -

    -

    +

    Before writing to the destination the node/attribute data is properly formatted according to the node type; all special XML symbols, such as < and &, are properly escaped. In order to guard against forgotten node/attribute names, empty node/attribute names are printed as ":anonymous". For well-formed output, make sure all node and attribute names are set to meaningful values. -

    -

    +

    If you want to save the whole document to a file, you can use the save_file function, which returns true on success. This is a simple example - of saving XML document to file (samples/save_file.cpp): -

    -

    + of saving XML document to file (samples/save_file.cpp): +

    -

    -
    // save document to file
    +

    // save document to file
     std::cout << "Saving result: " << doc.save_file("save_file_output.xml") << std::endl;
    -
    -

    -

    -

    +

    +

    To enhance interoperability pugixml provides functions for saving document to any object which implements C++ std::ostream interface. This allows you to save documents to any standard C++ stream (i.e. @@ -720,20 +559,15 @@ Most notably, this allows for easy debug output, since you can use std::cout stream as saving target. There are two functions, one works with narrow character streams, another handles wide character ones. -

    -

    - This is a simple example of saving XML document to standard output (samples/save_stream.cpp): -

    -

    +

    + This is a simple example of saving XML document to standard output (samples/save_stream.cpp): +

    -

    -
    // save document to standard output
    +

    // save document to standard output
     std::cout << "Document:\n";
     doc.save(std::cout);
    -
    -

    -

    -

    +

    +

    All of the above saving functions are implemented in terms of writer interface. This is a simple interface with a single function, which is called several times during output process with chunks of document data as input. In order @@ -741,16 +575,13 @@ should create an object which implements xml_writer_file interface and pass it to xml_document::save function. -

    -

    +

    This is a simple example of custom writer for saving document data to STL - string (samples/save_custom_writer.cpp); + string (samples/save_custom_writer.cpp); read the sample code for more complex examples: -

    -

    +

    -

    -
    struct xml_string_writer: pugi::xml_writer
    +

    struct xml_string_writer: pugi::xml_writer
     {
         std::string result;
     
    @@ -759,58 +590,38 @@
             result += std::string(static_cast<const char*>(data), size);
         }
     };
    -
    -

    -

    -

    +

    +

    While the previously described functions save the whole document to the destination, it is easy to save a single subtree. Instead of calling xml_document::save, just call xml_node::print function on the target node. You can save node contents to C++ IOstream object or custom writer in this way. - Saving a subtree slightly differs from saving the whole document; read the manual for + Saving a subtree slightly differs from saving the whole document; read the manual for more information. -

    -
    -
    - -

    - If you believe you've found a bug in pugixml, please file an issue via issue submission form. +

    + If you believe you've found a bug in pugixml, please file an issue via issue submission form. Be sure to include the relevant information so that the bug can be reproduced: the version of pugixml, compiler version and target architecture, the code that uses pugixml and exhibits the bug, etc. Feature requests and contributions can be filed as issues, too. -

    -

    +

    If filing an issue is not possible due to privacy or other concerns, you - can contact pugixml author by e-mail directly: arseny.kapoulkine@gmail.com. -

    -
    -
    - -

    + can contact pugixml author by e-mail directly: arseny.kapoulkine@gmail.com. +

    The pugixml library is distributed under the MIT license: -

    -
    -

    - Copyright (c) 2006-2010 Arseny Kapoulkine -

    -

    +

    + Copyright (c) 2006-2012 Arseny Kapoulkine +

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -

    -

    +

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -

    -

    +

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL @@ -818,28 +629,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -

    -
    -

    +

    This means that you can freely use pugixml in your applications, both open-source and proprietary. If you use pugixml in a product, it is sufficient to add an acknowledgment like this to the product distribution: -

    -

    +

    This software is based on pugixml library (http://pugixml.org).
    pugixml - is Copyright (C) 2006-2010 Arseny Kapoulkine. -

    -
    -
    -
    -

    -

    [1] All trademarks used are properties of their respective owners.

    -
    -
    - - - -

    Last revised: October 31, 2010 at 07:44:52 GMT

    - - + is Copyright (C) 2006-2012 Arseny Kapoulkine. +



    [1] All trademarks used are properties of their respective owners.

    -- cgit v1.2.3