使用 Boost 实现更好的 XML 格式设置

Better XML formatting using Boost?

本文关键字:XML 格式 设置 更好 Boost 实现 使用      更新时间:2023-10-16

我正在使用 Boost 属性树将我的类时刻导出为 XML 节点。它可以工作,但它只是将所有内容放在 1 行中。我希望它有缩进,例如:

<?xml version="1.0" encoding="utf-8"?>
<root>
<sensorconfigurations>
    <configuration>
        <name>SensorConfiguration1</name>
        <sensorid>1</sensorid>
        <signalindex>1</signalindex>
        <mappingscheme>mappingscheme1</mappingscheme>
        <soundpack>test1.wav</soundpack>
    </configuration>
    <configuration>
        <name>SensorConfiguration2</name>
        <sensorid>2</sensorid>
        <signalindex>2</signalindex>
        <mappingscheme>mappingscheme1</mappingscheme>
        <soundpack>test2.wav</soundpack>
    </configuration>
    <configuration>
        <name>SensorConfiguration3</name>
        <sensorid>3</sensorid>
        <signalindex>3</signalindex>
        <mappingscheme>mappingscheme2</mappingscheme>
        <soundpack>test3.wav</soundpack>
    </configuration>
</sensorconfigurations>
</root>

这可能吗?我是否缺少write_xml方法中的参数?

这是我的代码:

void SensorConfigurationBank::save()
{
using boost::property_tree::ptree;
ptree pt;
for(map<string, SensorConfiguration>:: iterator it = sensorConfigurations_.begin(); it != sensorConfigurations_.end(); ++it) 
{
    ptree myTree;
    myTree.put("name", it->second.getName());
    myTree.put("sensorid", it->second.getSensorID());
    myTree.put("signalindex", it->second.getsignalIndex());
    MappingScheme myScheme = it->second.getMScheme();
    myTree.put("mappingscheme", myScheme.getId());
    SoundPack mySound = it->second.getSound();
    myTree.put("soundpack", mySound.filepath_);
    pt.add_child("root.sensorconfigurations.configuration", myTree);
}
write_xml("SensorConfigurationBank2.xml", pt); 
}

如今,xml_writer_settings显然将字符串类型作为模板参数,因此:

boost::property_tree::xml_writer_settings<std::string> settings('t', 1);
write_xml(std::cout, pt, settings); 

会做这个伎俩。完整示例:

住在科里鲁

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <map>
#include <iostream>
struct SoundPack {
    std::string filepath_ = "soundpack.wav";
};
struct MappingScheme {
    std::string getId()    const { return "Id";    }
};
struct SensorConfiguration {
    std::string   getName()        const { return "Name";        }
    std::string   getSensorID()    const { return "SensorID";    }
    std::string   getsignalIndex() const { return "signalIndex"; }
    SoundPack     getSound()       const { return {};            }
    MappingScheme getMScheme()     const { return {};            }
};
void save(std::map<std::string, SensorConfiguration> sensorConfigurations_)
{
    using boost::property_tree::ptree;
    ptree pt;
    for(std::map<std::string, SensorConfiguration>:: iterator it = sensorConfigurations_.begin(); it != sensorConfigurations_.end(); ++it) 
    {
        ptree myTree;
        MappingScheme myScheme = it->second.getMScheme();
        SoundPack mySound = it->second.getSound();
        myTree.put("name",          it->second.getName());
        myTree.put("sensorid",      it->second.getSensorID());
        myTree.put("signalindex",   it->second.getsignalIndex());
        myTree.put("mappingscheme", myScheme.getId());
        myTree.put("soundpack",     mySound.filepath_);
        pt.add_child("root.sensorconfigurations.configuration", myTree);
    }
    boost::property_tree::xml_writer_settings<std::string> settings('t', 1);
    write_xml(std::cout, pt, settings); 
}
int main() {
    save({
            { "first",  SensorConfiguration {} },
            { "second", SensorConfiguration {} },
            { "third",  SensorConfiguration {} },
            { "fourth", SensorConfiguration {} }
        });
}

输出:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <sensorconfigurations>
        <configuration>
            <name>Name</name>
            <sensorid>SensorID</sensorid>
            <signalindex>signalIndex</signalindex>
            <mappingscheme>Id</mappingscheme>
            <soundpack>soundpack.wav</soundpack>
        </configuration>
        <configuration>
            <name>Name</name>
            <sensorid>SensorID</sensorid>
            <signalindex>signalIndex</signalindex>
            <mappingscheme>Id</mappingscheme>
            <soundpack>soundpack.wav</soundpack>
        </configuration>
        <configuration>
            <name>Name</name>
            <sensorid>SensorID</sensorid>
            <signalindex>signalIndex</signalindex>
            <mappingscheme>Id</mappingscheme>
            <soundpack>soundpack.wav</soundpack>
        </configuration>
        <configuration>
            <name>Name</name>
            <sensorid>SensorID</sensorid>
            <signalindex>signalIndex</signalindex>
            <mappingscheme>Id</mappingscheme>
            <soundpack>soundpack.wav</soundpack>
        </configuration>
    </sensorconfigurations>
</root>