Class for reading and storing values to XML files.
The whole XML file is stored in memory, along with the created document nodes. This class aims to provide simplified support for reading attributes from XML files, usually meant to be used as configurations, similar to Boost.PropertyTree
. It uses the RapidXml XML library for parsing. For any specifics on parsing XML files, refer to RapidXml documentation.
The intended use is very simple: .get<int>("element1.element2.attribute")
returns the value of the attribute
, nested in element2
inside element1
inside XML root element. The complementary .set("element3.attribute", 34.4)
sets the attribute to 34.4
.
- Warning
- The attribute path does not contain the root XML element for brevity, as XML standard requires each document to have unique root element and that part of the path would have always been the same.
Usage example:
XML xml(
"test/testdata/test_conf.xml");
if (xml.exists("element1.attr")) {
int a = xml.get<int>("element1.attr");
std::cout << a << std::endl;
}
xml.set("test.abc.xyz", 12.4543);
double r = xml.get<double>("test.abc.xyz");
xml.set("element1.attr", 12, true);
std::cout << xml << std::endl;
xml.load("test/testdata/another_file.xml");
Definition at line 40 of file XML_fwd.hpp.
|
| XML ()=default |
| Creates an XML reader linked to no document. More...
|
|
| XML (const XML &) |
| Creates a copy of the XML document. More...
|
|
| XML (const std::string &filename) |
| Constructs a XML reader by reading the file given by filename . More...
|
|
void | load (const std::string &filename) |
| Loads XML document from a file given by filename . More...
|
|
bool | exists (const std::string &path) const |
| Returns true if the an attribute specified by path exists and false otherwise. More...
|
|
template<typename T > |
T | get (const std::string &path) const |
| Reads a values from an attribute specified by path . More...
|
|
template<typename T > |
void | set (const std::string &path, const T &value, bool overwrite=false) |
| Saves a value to the attribute pointed to by path . More...
|
|
std::vector< std::pair< std::string, std::string > > | getAll () const |
| Returns all pairs (path, attribute_value) . More...
|
|
rapidxml::xml_document< char > & | documentRoot () |
| Access to underlying XML root element from RapidXml library. More...
|
|
const rapidxml::xml_document< char > & | documentRoot () const |
| Const version of XML::documentRoot. More...
|
|
|
void | loadFileHelper (const std::string &file) |
| Function for opening files, called by XML::XML(const std::string&) and XML::load(). More...
|
|
void | loadFromStoredContents () |
| Loads the XML document from a stored null-terminated file_contents . More...
|
|
char * | getString (const std::vector< std::string > &path, const std::string &attribute_name) const |
| Reads the contents of the attribute specified by path as a string. More...
|
|
void | setString (const std::vector< std::string > &path, const std::string &attribute_name, const std::string &content) |
| Writes the contents of string content to the attribute specified by path . More...
|
|
void | getAllRecursive (const rapidxml::xml_node<> *node, std::string path, std::vector< std::pair< std::string, std::string >> &all_attr) const |
| Fills the all_attr array with all pairs (path, value) pairs that are descendants of node node at path path . More...
|
|