2016-06-29 103 views
1

我想解析包含特定属性的节点的随机xml文件,并检索此属性的所有值。用例是有许多具有不同节点的XML,但要检索的属性始终是已知的。C++ Boost循环通过未知的ptree和属性列表值

下面是一个文件的例子:

<node> 
<container> 
    <object attribute="value" /> 
    <object attribute="value" /> 
<container/> 
<supercontainer> 
    <subcontainer> 
    <otherobject attribute="value" /> 
    <subcontainer/> 
<supercontainer/> 
</node> 

这是我现在使用升压property_tree但我不知道该怎么在循环做:

ptree pt; 
read_xml(xml_file, pt); 
BOOST_FOREACH(boost::property_tree::ptree::value_type &ele, pt) 
{ 
    //NO IDEA 
} 

思路欢迎。

感谢

回答

0

OK,我解决它使用下列内容:

void recurseManifest(ptree &pt, int lvl) 
{ 
    for (ptree::iterator current = pt.begin(); current != pt.end();) 
    { 
    try 
    { 
     std::cout << current->second.get<std::string>("<xmlattr>.attribute") << std::endl; 
    } 
    catch(const boost::property_tree::ptree_bad_path &err) 
    { 
     std::cerr << err.what() << std::endl;  
    } 
    assets = recurseManifest(current->second, lvl++); 
    ++current; 
    } 
}