BOOST存储 XML格式化问题

存储后,格式错乱

http://www.cnblogs.com/coding-my-life/p/4070201.html

boost::property_tree::xml_writer_settings<char> settings('\t',1);
write_xml(filename, pt,std::local(),settings);

意思是缩进1个\t,结果报了一大堆错误:
由于我没有启用C++11,改为这样写

boost::property_tree::xml_writer_settings<string> settings =
boost::property_tree::xml_writer_make_settings<string> (' ', 4);
write_xml( DEFAULTCONFIG,pt,std::locale(),settings );

问题解决。

存储后,多许多空行

https://stackoverflow.com/questions/6572550/boostproperty-tree-xml-pretty-printing

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.
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:
<?xml version="1.0" ?>
<config>
    <net>
        <listenPort>10420</listenPort>
    </net>
</config>

after running the program file2.xml contains:

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



    <net>



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

解决:

The solution was to add the trim_whitespace flag to the call to 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 here but the current maintainer of the library (Sebastien Redl) was kind enough to answer and point me to it.

read_xml("file.xml", pt, boost::property_tree::xml_parser::trim_whitespace );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值