C++ 使用Boost改进XML格式?
我使用Boost属性树将类实例导出为XML节点。 它可以工作,但它只是把一切放在一行。我希望它有缩进,比如:C++ 使用Boost改进XML格式?,c++,xml,boost,ptree,C++,Xml,Boost,Ptree,我使用Boost属性树将类实例导出为XML节点。 它可以工作,但它只是把一切放在一行。我希望它有缩进,比如: <?xml version="1.0" encoding="utf-8"?> <root> <sensorconfigurations> <configuration> <name>SensorConfiguration1</name> <sensorid>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>
传感器配置1
1.
1.
映射方案1
test1.wav
传感器配置2
2.
2.
映射方案1
test2.wav
传感器配置3
3.
3.
映射模式2
test3.wav
这有可能吗?我是否在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);
}
void SensorConfigurationBank::save()
{
使用boost::property_tree::ptree;
ptree-pt;
对于(map::iterator it=sensorConfigurations_uz.begin();it!=sensorConfigurations_z.end();+it)
{
桃金娘树;
myTree.put(“name”,it->second.getName());
myTree.put(“sensorid”,it->second.getSensorID());
myTree.put(“signalindex”,it->second.getsignalIndex());
MappingScheme myScheme=it->second.getMScheme();
put(“mappingscheme”,myScheme.getId());
SoundPack mySound=it->second.getSound();
myTree.put(“soundpack”,mySound.filepath);
pt.add_child(“root.sensorconfigurations.configuration”,myTree);
}
编写xml(“SensorConfigurationBank2.xml”,pt);
}
如今,xml\u writer\u settings
显然采用字符串类型作为模板参数,因此:
boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
write_xml(std::cout, pt, settings);
boost::property\u tree::xml\u writer\u设置('\t',1);
编写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 {} }
});
}
#包括
#包括
#包括
#包括
结构声包{
std::string filepath=“soundpack.wav”;
};
结构映射方案{
std::string getId()常量{return“Id”;}
};
结构传感器配置{
std::string getName()常量{返回“Name”;}
std::string getSensorID()常量{return“SensorID”;}
std::string getsignalIndex()常量{return“signalIndex”;}
SoundPack getSound()常量{return{};}
MappingScheme getMScheme()常量{return{};}
};
无效保存(标准::映射传感器配置)
{
使用boost::property_tree::ptree;
ptree-pt;
对于(std::map::iterator it=sensorConfigurations_uz.begin();it!=sensorConfigurations_z.end();++it)
{
桃金娘树;
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());
put(“mappingscheme”,myScheme.getId());
myTree.put(“soundpack”,mySound.filepath);
pt.add_child(“root.sensorconfigurations.configuration”,myTree);
}
boost::property\u tree::xml\u writer\u设置('\t',1);
编写xml(std::cout、pt、settings);
}
int main(){
拯救({
{“第一”,传感器配置{},
{“第二”,传感器配置{},
{“第三”,传感器配置{},
{“第四”,传感器配置{}
});
}
输出:
名称
传感器
信号指数
身份证件
声音包
名称
传感器
信号指数
身份证件
声音包
名称
传感器
信号指数
身份证件
声音包
名称
传感器
信号指数
身份证件
声音包
如今,xml\u writer\u settings
显然采用字符串类型作为模板参数,因此:
boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
write_xml(std::cout, pt, settings);
boost::property\u tree::xml\u writer\u设置('\t',1);
编写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 {} }
});
}
#包括
#包括
#包括
#包括
结构声包{
std::string filepath=“soundpack.wav”;
};
结构映射方案{
std::string getId()常量{return“Id”;}
};
结构传感器配置{
std::string getName()常量{返回“Name”;}
std::string getSensorID()常量{return“SensorID”;}
std::string getsignalIndex()常量{return“signalIndex”;}
SoundPack getSound()常量{return{};}
MappingScheme getMScheme()常量{return{};}
};
无效保存(标准::映射传感器配置)
{
使用boost::property_tree::ptree;
ptree-pt;
对于(std::map::iterator it=sensorConfigurations_uz.begin();it!=sensorConfigurations_z.end();++it)
{
桃金娘树;
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());
put(“mappingscheme”,myScheme.getId());
myTree.put(“soundpack”,mySound.filepath);
pt.add_child(“root.sensorconfigurations.configuration”,myTree);
}
boost::property\u tree::xml\u writer\u设置('\t',1);
编写xml(std::cout、pt、settings);
}
int main(){
拯救({
{“第一”,传感器配置{},
{“第二”,传感器配置{},
{“第三”,传感器配置{},
{“第四”,传感器配置{}
});
}
输出:
名称
传感器
信号指数
身份证件
声音包
名称
传感器
信号指数
身份证件
声音包
名称
传感器
s