xmlxml-parsingrapidxml

Rapidxml how to change the tag format when outputting an empty element


I use the rapidxml library to read/parse the content from a xml file, make some changes in the dom's content and then save the dom to the file again.

When I save the contents of the xml_document to the file, the elements which contain an empty string are saved as <empty_tag/> but instead I want them to be saved as <empty_tag><empty_tag/>. I it possible to change this with rapidxml?

The flow is something like:

//read the xml content
rapidxml::xml_document<> dom;
std::ifstream i_xmlfile('path');
std::vector<char> xml_content = std::vector<char>(std::istreambuf_iterator<
        char>(i_xmlfile), std::istreambuf_iterator<char>());
xml_content.push_back('\0');
dom.parse<0 | rapidxml::parse_no_data_nodes> (&xml_content[0]);

... process nodes here ...

//save the xml content
std::ofstream o_xmlfile;
o_xmlfile.open('path');
o_xmlfile << dom;

Solution

  • After a more thoughtful search in the library code I conclude that it is not possible to change this format only if you edit the library.

    So, in order to make it work, I changed some lines in file rapidxml_print.hpp for method print_element_node() (lines 283 in rapidxml version 1.13):

    // Print childless node tag ending *out = Ch('/'), ++out; *out = Ch('>'), ++out;

    changed with:

    *out = Ch('>'), ++out; // Print node end *out = Ch('<'), ++out; *out = Ch('/'), ++out; out = copy_chars(node->name(), node->name() + node->name_size(), out); *out = Ch('>'), ++out;