2011-07-04 141 views
27

我正在使用boost :: property_tree在我的应用程序中读取和写入XML配置文件。 但是,当我写文件的输出看起来很丑陋,文件中有很多空行。 问题是,它应该由人类编辑,所以我想获得更好的输出。boost :: property_tree XML漂亮打印

例如,我写了一个小测试程序:

#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包含:

<?xml version="1.0" ?> 
<config> 
    <net> 
     <listenPort>10420</listenPort> 
    </net> 
</config> 

运行程序file2.xml后包含:

<?xml version="1.0" encoding="utf-8"?> 
<config> 



    <net> 



     <listenPort>10420</listenPort> 
    </net> 
</config> 

是有办法获得更好的输出,除了通过手动输出和删除空行吗?

+2

的boost :: property_tree使用名为RapidXML,http://rapidxml.sourceforge.net/ XML解析器。 boost :: property_tree和RapidXML均由Marcin Kalicinski维护。我建议你直接与他联系。您可以在RapidXML主页上找到他的邮件地址。 – user763305

+0

感谢ildjarn的编辑,但空行在这里是有原因的!顺便说一句问问题的维护者,我会后的答案,如果有一个 – foke

回答

39

的解决方案是在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; 
} 

的标志被记录here但库(塞巴斯蒂安·雷德尔)的当前的维护者还跟回答和我指向它。

+1

警告:trim_whitespace不仅修剪空白的XML,而且空格中的任何元素不包含其他元素:' XX'读就好像它是' xx'。 –

+7

奇怪的是,需要改变* read *设置才能得到这个(特别是在@AndreasHaferburg评论之后)。反正在升压当前版本的一个需要使用'xml_writer_settings '(不'char')。 – alfC

+0

更新“这里”链接:http://www.boost.org/doc/libs/1_58_0/doc/html/boost/property_tree/xml_parser/read_xml_idp82929296.html – alfC

3

,这个问题很老了,但我又研究了你的问题,最近,因为它得到了很多现在更糟的是property_tree转换换行符到

在我看来这是一个错误,因为元素,只包含空格 - 换行符,空格和制表符,被视为文本元素。 trim_whitespace只是一个bandaid,并将property_tree中的所有空白标准化。

我报道过这里的错误,还附上了为.diff修复的情况下,trim_whitespace这种行为在升压1.59不使用: https://svn.boost.org/trac/boost/ticket/11600

2

对于那些想:

boost::property_tree::xml_writer_settings<char> settings('\t', 1); 

与升压编译-1.60.0在VisualStudio的2013,你可能会得到:

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 

然后这里就结束了:

https://svn.boost.org/trac/boost/ticket/10272

解决发现的工作就是使用的std :: string模板。

pt::write_xml(file_name, params, std::locale(), pt::xml_writer_make_settings<std::string>(' ', 4)); 

如下所述:

https://stackoverflow.com/a/35043551/7170333