使用boost的property_tree写xml文件格式没有立体感
最近使用property_tree做xml文件的读写,读的时候是没有什么问题,但是写入的时候,会出现格式没立体感的现象,要手动编辑才可以调整过来。举个例子,比如xml文件的格式本来应该是这样:
<?xml version="1.0" encoding="utf-8"?>
<debug name = "debugname">
<file name = "debug.log"/>
<modules type = "internal"/>
</debug>
但是用property_tree的write_xml函数写入时,会变成
<?xml version="1.0" encoding="utf-8"?>
<debug name = "debugname"><file name = "debug.log"/><modules type = "internal"/></debug>
虽然不影响文件解析,但是总会让人很烦,手动编辑的时候没有层次感,每次都要手动调整回来。
ps:
我的文件写入代码是:
ptree pt;
read_xml("debug_settings.xml", pt);
BOOST_FOREACH(ptree::value_type &v1, pt.get_child("debug"))
{
if (v1.first == "<xmlattr>")
{
cout<<"debug name="<<v1.second.get<string>("name")<<endl;
}
else if (v1.first == "file")
{
cout<<"file name="<<v1.second.get<string>("<xmlattr>.name")<<endl;
}
else if (v1.first == "modules")
{
cout<<"module type:"<<v1.second.get<string>("<xmlattr>.type")<<endl;
}
}
boost::property_tree::xml_writer_settings<char> settings('\t', 1);
pt.put("debug.chenyp", "test");
write_xml("debug_settings.xml", pt);