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.
@@ -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:
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:
+
+ 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:
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.
+
+ 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().
+
+ It is common to store data as text contents of some node - i.e. <node><description>Thisisanode</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_textxml_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:
+
+
boolxml_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:
+
+
constchar_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:
+
+ 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_nodexml_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):
+