我使用rapidxml库从xml文件中读取parse内容,对dom的内容进行一些修改,然后再将dom保存到文件中。
当我把xml_document的内容保存到文件中时,包含空字符串的元素被保存为 <empty_tag/>
但我希望它们被保存为 <empty_tag><empty_tag/>
. 我可以用rapidxml来改变它吗?
流程是这样的。
//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;
在库的代码中,经过比较仔细的查找,我得出的结论是,只有在编辑库的时候,才可以改变这种格式。
所以,为了让它工作,我修改了文件rapidxml_print.hpp中的一些行,方法是 print_element_node()
(在rapidxml 1.13版本中为283行)。
// Print childless node tag ending
*out = Ch('/'), ++out;
*out = Ch('>'), ++out;
改为:
*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;
我有一个打了补丁的叉子,实现了明确声明一个自闭标签。https:/sourceforge.netptacklelib3dparty--rapidxmlHEADtreebranches。
例如:
for (rapidxml::xml_node<> * node = ...; node; node = node->next_sibling())
{
node->flags(node->flags() & ~rapidxml::node_self_closed_tag);
}