ヘルパー関数またはクラスを記述します。
#include <iostream>
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
class Node_Adder
{
public:
Node_Adder(rapidxml::xml_document<> & doc) : m_doc(doc) { }
rapidxml::xml_node<> * operator()(rapidxml::xml_node<> * parent,
char const * node_name)
{
char * name = m_doc.allocate_string(node_name);
rapidxml::xml_node<> * child = m_doc.allocate_node(rapidxml::node_element, name);
parent->append_node(child);
return child;
}
protected:
private:
rapidxml::xml_document<> & m_doc;
};
void stackoverflow()
{
rapidxml::xml_document<> doc;
rapidxml::xml_node<char> * decl = doc.allocate_node(rapidxml::node_declaration);
decl->append_attribute(doc.allocate_attribute("version", "1.0"));
decl->append_attribute(doc.allocate_attribute("encoding", "UTF-8"));
doc.append_node(decl);
rapidxml::xml_node<> * root = doc.allocate_node(rapidxml::node_element, "root");
doc.append_node(root);
Node_Adder add(doc);
add(add(add(root, "very"), "long"), "subtree");
rapidxml::print(std::ostreambuf_iterator<char>(std::cout), doc);
}
印刷する
<?xml version="1.0" encoding="UTF-8"?>
<root>
<very>
<long>
<subtree/>
</long>
</very>
</root>