Provide mapping of cxml to xmlpp in write_to_xmlpp_document.
[libcxml.git] / src / cxml.cc
index 8bfb923e60115172d76d9902ca0c6dee0dcb8d24..5db07ea6c98f23abb3fb08367f93239cd0bda0f4 100644 (file)
@@ -71,9 +71,12 @@ cxml::Node::read (xmlpp::Node const * node)
 }
 
 void
-cxml::Node::write (xmlpp::Node* parent) const
+cxml::Node::write (xmlpp::Node* parent, map<cxml::ConstNodePtr, xmlpp::Node*>* mapping) const
 {
        xmlpp::Element* node = parent->add_child (_name, _namespace_prefix);
+       if (mapping) {
+               (*mapping)[shared_from_this()] = node;
+       }
 
        if (!_content.empty ()) {
                node->add_child_text (_content);
@@ -319,10 +322,13 @@ cxml::read_string (string s)
        return node;
 }
 
-static void
-write_to_xmlpp_document (cxml::ConstNodePtr node, xmlpp::Document& doc)
+void
+cxml::write_to_xmlpp_document (cxml::ConstNodePtr node, xmlpp::Document& doc, map<cxml::ConstNodePtr, xmlpp::Node*>* mapping)
 {
        xmlpp::Element* root = doc.create_root_node (node->name ());
+       if (mapping) {
+               (*mapping)[node] = root;
+       }
 
        cxml::KeyValueList namespace_declarations = node->namespace_declarations ();
        for (cxml::KeyValueList::const_iterator i = namespace_declarations.begin(); i != namespace_declarations.end(); ++i) {
@@ -331,7 +337,7 @@ write_to_xmlpp_document (cxml::ConstNodePtr node, xmlpp::Document& doc)
 
        cxml::NodeList children = node->children ();
        for (cxml::NodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
-               (*i)->write (root);
+               (*i)->write (root, mapping);
        }
 }