0

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;
Adi Fatol
  • 956
  • 15
  • 24
  • Why does it matter? `` and `` are _exactly_ the same XML and every XML parser will treat them the same (to the extent that consuming code won't know which form was used in the original input, the code will just see an empty element). – Ian Roberts Sep 15 '14 at 13:24
  • Yes, I know that both forms mean the same thing, but these output files are read by some 3rd party service that supports only the second version of the format (``). – Adi Fatol Sep 15 '14 at 13:51

2 Answers2

0

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;

Adi Fatol
  • 956
  • 15
  • 24
0

I've a fork with patched implementation to explicitly declare a self-closed tag: https://github.com/andry81-3dparty/rapidxml/branches

Example:

for (rapidxml::xml_node<> * node = ...; node; node = node->next_sibling())
{
  node->flags(node->flags() & ~rapidxml::node_self_closed_tag);
}
Andry
  • 2,273
  • 29
  • 28