boost::property_tree XML pretty printing

前端 未结 3 1011
独厮守ぢ
独厮守ぢ 2020-12-02 13:12

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

3条回答
  •  盖世英雄少女心
    2020-12-02 14:11

    The solution was to add the trim_whitespace flag to the call to read_xml:

    #include 
    #include 
    
    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 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.

提交回复
热议问题