3

我正在尝试使用 pugixml 修改通过 boost::serialization 创建并由其他应用程序使用的 xml 配置,因此我只需要更改文档的几个片段并使所有其他部分保持不变。

一些节点可能会以<value></value>. 加载和保存(使用 pugi)后,此节点变为<value />. 在此 boost::serialization 之后无法解析此类文件。

加载选项parse_ws_pcdata_singleparse_ws_pcdata仅在两者之间有空格时才起作用。

我也没有找到在表单中保存空节点的保存选项<value></value>

有什么方法可以保留零文本的开始和结束标签?

4

3 回答 3

2

没有现成的选择。

更改 pugixml 以输出您需要的 XML 很容易(可能比修复 boost::serialization... 更容易):

在 pugixml.cpp 的第 3249 行附近,有以下代码:

        else if (!node.first_child())
            writer.write(' ', '/', '>', '\n');

如果您使用缩进格式,只需删除这两行(如果您使用的是 format_raw,上面有类似的代码)。

于 2014-06-06T02:58:03.043 回答
2

Pugixml 已更新,对此的答案已更改

这适用于 Pugixml 1.6 版

要为所有节点生成结束标签,

修改 pugixml.cpp:第 3503 行

PUGI__FN bool node_output_start(xml_buffered_writer& writer, xml_node_struct* node, unsigned int flags)

PUGI__FN void node_output_end(xml_buffered_writer& writer, xml_node_struct* node);
PUGI__FN bool node_output_start(xml_buffered_writer& writer, xml_node_struct* node, unsigned int flags)

修改 pugixml.cpp:第 3516 行

writer.write(' ', '/', '>');

writer.write('>');
node_output_end(writer, node);
于 2015-08-28T02:56:08.110 回答
1

如果有人仍然遇到这个问题,您无需更改来源。编写树时只需使用此标志:

pugi::format_no_empty_element_tags

请参阅https://pugixml.org/docs/manual.html#saving.options中的文档

于 2021-07-01T11:16:05.543 回答