C++ boost::property_tree XML 漂亮打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6572550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
boost::property_tree XML pretty printing
提问by foke
I'm using boost::property_tree to read and write XML configuration files in my application. But when I write the file the output looks kind of ugly with lots of empty lines in the file. The problem is that it's supposed to be edited by humans too so I'd like to get a better output.
我正在使用 boost::property_tree 在我的应用程序中读取和写入 XML 配置文件。但是当我写文件时,输出看起来有点难看,文件中有很多空行。问题是它也应该由人类编辑,所以我想获得更好的输出。
As an example I wrote a small test program :
作为一个例子,我写了一个小测试程序:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
int main( void )
{
using boost::property_tree::ptree;
ptree pt;
// reading file.xml
read_xml("file.xml", pt);
// writing the unchanged ptree in file2.xml
boost::property_tree::xml_writer_settings<char> settings('\t', 1);
write_xml("file2.xml", pt, std::locale(), settings);
return 0;
}
file.xml contains:
file.xml 包含:
<?xml version="1.0" ?>
<config>
<net>
<listenPort>10420</listenPort>
</net>
</config>
after running the program file2.xml contains:
运行程序后 file2.xml 包含:
<?xml version="1.0" encoding="utf-8"?>
<config>
<net>
<listenPort>10420</listenPort>
</net>
</config>
Is there a way to have a better output, other than going manually through the output and deleting empty lines?
除了手动通过输出并删除空行之外,有没有办法获得更好的输出?
回答by foke
The solution was to add the trim_whitespace
flag to the call to read_xml
:
解决方案是将trim_whitespace
标志添加到调用中read_xml
:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
int main( void )
{
// Create an empty property tree object
using boost::property_tree::ptree;
ptree pt;
// reading file.xml
read_xml("file.xml", pt, boost::property_tree::xml_parser::trim_whitespace );
// writing the unchanged ptree in file2.xml
boost::property_tree::xml_writer_settings<char> settings('\t', 1);
write_xml("file2.xml", pt, std::locale(), settings);
return 0;
}
The flag is documented herebut the current maintainer of the library (Sebastien Redl) was kind enough to answer and point me to it.
该标志已记录在此处,但该库的当前维护者 (Sebastien Redl) 非常友好地回答并指出了它。
回答by tstrunk
This question is quite old, but I investigated your problem again, lately, because it got a lot worse now that property_tree translates newlines to
这个问题已经很老了,但我最近再次调查了您的问题,因为现在 property_tree 将换行符转换为
In my opinion this is a bug, because elements, which contains only whitespace - newlines, spaces and tabs, are treated as text elements. trim_whitespace is only a bandaid and normalizes ALL whitespace in the property_tree.
在我看来,这是一个错误,因为仅包含空格(换行符、空格和制表符)的元素被视为文本元素。trim_whitespace 只是一个bandaid 并规范化property_tree 中的所有空白。
I reported the bug over here and also attached a .diff to fix this behaviour in Boost 1.59 in case trim_whitespace is not used: https://svn.boost.org/trac/boost/ticket/11600
我在这里报告了这个错误,并附上了一个 .diff 来修复 Boost 1.59 中的这个行为,以防不使用 trim_whitespace:https://svn.boost.org/trac/boost/ticket/11600
回答by bitminer
For those trying:
对于那些尝试:
boost::property_tree::xml_writer_settings<char> settings('\t', 1);
Compiling with boost-1.60.0 in VisualStudio 2013 you may get:
在 VisualStudio 2013 中使用 boost-1.60.0 进行编译,您可能会得到:
vmtknetworktest.cpp(259) : see reference to class template instantiation 'boost::property_tree::xml_parser::xml_writer_settings<char>' being compiled
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(38): error C2039: 'value_type' : is not a member of '`global namespace''
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(38): error C2146: syntax error : missing ';' before identifier 'Ch'
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(38): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(40): error C2061: syntax error : identifier 'Ch'
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(49): error C2146: syntax error : missing ';' before identifier 'indent_char'
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(50): error C2825: 'Str': must be a class or namespace when followed by '::'
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(50): error C2039: 'size_type' : is not a member of '`global namespace''
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(50): error C2146: syntax error : missing ';' before identifier 'indent_count'
install\include\boost-1_60\boost/property_tree/detail/xml_parser_writer_settings.hpp(50): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
vmtknetworktest.cpp(259): error C2661: 'boost::property_tree::xml_parser::xml_writer_settings<char>::xml_writer_settings' : no overloaded function takes 3 arguments
Then end up here:
然后到这里结束:
https://svn.boost.org/trac/boost/ticket/10272
https://svn.boost.org/trac/boost/ticket/10272
Solution to found to work is to use std::string in template.
找到工作的解决方案是在模板中使用 std::string 。
pt::write_xml(file_name, params, std::locale(), pt::xml_writer_make_settings< std::string >(' ', 4));
as described here:
如此处所述: