zenXML
Straightforward C++ XML Processing
|
Handle conversion of arbitrary types to and from XML elements. More...
#include "string_utf8.h"
#include "string_tools.h"
#include "zenxml_dom.h"
Go to the source code of this file.
Namespaces | |
namespace | zen |
The zenXML namespace. | |
Functions | |
template<class T > | |
bool | zen::readValue (const XmlElement &input, T &value) |
Convert XML element to user data. | |
template<class T > | |
void | zen::writeValue (const T &value, XmlElement &output) |
Convert user data into an XML element. | |
template<class T > | |
bool | zen::readText (const std::string &input, T &value) |
Convert text to user data - used by XML elements and attributes. | |
template<class T > | |
void | zen::writeText (const T &value, std::string &output) |
Convert user data into text - used by XML elements and attributes. |
Handle conversion of arbitrary types to and from XML elements.
It is not required to call these functions directly. They are implicitly used by zen::XmlElement::getValue(), zen::XmlElement::setValue(), zen::XmlElement::getAttribute() and zen::XmlElement::setAttribute().
By default the following conversions to user types are automatically supported:
You can add support for additional types via template specialization.
Specialize zen::readValue() and zen::writeValue() to enable conversion from structured user types to XML elements. Specialize zen::readText() and zen::writeText() to enable conversion from string-convertible user types to std::string. Latter is generally more useful as it does not only allow converting from XML elements to user data, but also from and to XML attributes.
Example: type "bool"
namespace zen { template <> inline void writeText(const bool& value, std::string& output) { output = value ? "true" : "false"; } template <> inline bool readText(const std::string& input, bool& value) { std::string tmp = input; zen::trim(tmp); if (tmp == "true") value = true; else if (tmp == "false") value = false; else return false; return true; } }