Boost XML - get next node



The following code attempts to get data from an xml file and add it to a custom SensorConfiguration class, and then add that to a map of these SensorConfigurations


My problem is that it always takes the first sensor configuration in the second foreach loop. I know why, but I don't know the correct syntax, and I'm not sure how to google this.


Here's the XML:



<?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>


Here's the entire function constructor-method:



SensorConfigurationBank::SensorConfigurationBank()
{
string m_level;
using boost::property_tree::ptree;
ptree pt;
read_xml("SensorConfigurationBank.xml", pt);
BOOST_FOREACH(ptree::value_type &v,
pt.get_child("root.sensorconfigurations"))
{
SensorConfiguration newSensorConf;
BOOST_FOREACH(ptree::value_type &w,
pt.get_child("root.sensorconfigurations.configuration"))
{
if(w.first == "name")
{
newSensorConf.setName(w.second.data());
}
if(w.first == "sensorid")
{
string stringToInt = w.second.data();
istringstream iss(stringToInt);
int value;
iss >> value;
newSensorConf.setSensorID(value);
}
if(w.first == "signalindex")
{
string stringToInt = w.second.data();
istringstream iss(stringToInt);
int value;
iss >> value;
newSensorConf.setSignalIndex(value);
}
if(w.first == "mappingscheme")
{
newSensorConf.setMappingScheme(getMappingScheme(w.second.data()));
}
if(w.first == "soundpack")
{
newSensorConf.setSoundPack(w.second.data());
}
}
sensorConfigurations_.insert(make_pair(newSensorConf.getName(), newSensorConf));
}
//save();
}


I know it's slightly incomprehensible, but here's the important part:



BOOST_FOREACH(ptree::value_type &w,
pt.get_child("root.sensorconfigurations.configuration"))


Instead of getting the specific child 'configuration', I want it to get to the child that the first loop is looking at, so it looks at configuration #2 instead of getting configuration #1 up to an infinite number of times.


It's basically just the last line of code I pasted above that needs to be changed somehow, then I think it will work! Thanks in advance.


No comments:

Post a Comment