Boost logo

Boost Users :

From: Johan Nilsson (r.johan.nilsson_at_[hidden])
Date: 2006-06-16 04:02:11


Demetrius Cassidy wrote:
> "Johan Nilsson" wrote in message news:...
>>
>> Well, something along the lines of this should work (caution: not
>> tested): ptree technologies; /* load xml */ for
>> (ptree::const_iterator iter = technologies.begin(); iter !=
>> technologies.end(); ++iter) { std::cout << "ID: " <<
>> (*iter).get<std::string>("<xmlattr>.ID") << '\n' }
>> HTH // Johan
>
> Unfortunally this does not work, *iter returns ptree::value_type, so
> even if I could do
> std::cout << "ID: " <<
> (*iter).second.get<std::string>("<xmlattr>.ID") << '\n',
> it will throw "ptree_bad_path" because it can't find the key.
> It will also throw with get("Technologies.Logistics") and
> get_child(...).

I said I didn't test it - it required a little tweaking:

---
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <sstream>
#include <iostream>
using namespace boost::property_tree;
std::stringstream data(
    "<Technologies>"
    " <Logistics ID=\"Damage Diagnostics\">"
    " <DisplayName>Damage Diagnostics</DisplayName>"
    " <Cost>1500</Cost>"
    " <Description>Increases repair ability by 10%</Description>"
    " <RepairAbility>10</RepairAbility>"
    " <Requires>Advanced Starship Repair</Requires>"
    " <Category>Logistics</Category>"
    " <Model>lrgbuilding0</Model>"
    " <AIValue>30</AIValue>"
    " </Logistics>"
    " <Logistics ID=\"Basic Self-Repair\">"
    " <DisplayName>Basic Self-Repair</DisplayName>"
    " <Cost>3000</Cost>"
    " <Description>Increases repair ability by 10%</Description>"
    " <RepairAbility>10</RepairAbility>"
    " <Requires>Damage Diagnostics</Requires>"
    " <Category>Logistics</Category>"
    " <Model>controlgrav0</Model>"
    " <AIValue>40</AIValue>"
    " </Logistics>"
"</Technologies>");
ptree pt;
read_xml(data, pt);
ptree technologies = pt.get_child("Technologies");
for (ptree::const_iterator iter = technologies.begin(); iter != 
technologies.end(); ++iter)
{
    std::string const & name = (*iter).first;
    ptree const& logistics = (*iter).second;
    std::cout << name << ": ID = " << 
logistics.get<std::string>("<xmlattr>.ID") << '\n';
}
---
// Johan

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net