Parsing XML File with Boost C++

你离开我真会死。 提交于 2019-12-08 17:29:46
Blacktempel

Boost Documentation:

The attributes of an XML element are stored in the subkey . There is one child node per attribute in the attribute node. Existence of the node is not guaranteed or necessary when there are no attributes.

<module value = "abc"/>
//One way would be this:
boost::get<std::string>("module.<xmlattr>.value");

One more way (untested), which appears to be better:

BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("a.modules"))
{
    std::cout << v.second.get_child("<xmlattr>.type").data() << std::endl;
    std::cout << v.second.get_child("<xmlattr>.Reference").data() << std::endl;
}

One more taken from here.

//Parse XML...
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child("a.modules"))
{
    const boost::property_tree::ptree &attributes = v.second.get_child("<xmlattr>", boost::property_tree::ptree());
    BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, attributes)
    {
        std::cout << v.first.data() << std::endl;
        std::cout << v.second.data() << std::endl;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!