【发布时间】:2015-01-23 23:50:54
【问题描述】:
我正在使用 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
-
我知道我现在应该做什么了 :) 但由于某种原因,它说我的 write_xml 与参数列表不匹配,即使我写的东西和那个人完全相同在链接中。
-
@MathiasSiigNørregaard 只是编辑您的问题?