使用 boost 解析 XML 文件中的节点属性

参考来源

http://stackoverflow.com/questions/14010473/parsing-xml-attributes-with-boost


花了两小时,总算解析出来,为了避免遗忘做个笔记


XML 文件 TaskCfg.xml 内容如下


<Tasks>
    <EverydayTasks max_count_oneday="5">
        <Task id="1000" title="testtestetse" target_count="5" awards="10000|1,10000|2, 20000|3">
            <TargetCondition type="get_goods" item_id="3001" />
        </Task>
        <Task id="1001" title="累计获得[target_count]个[item_id]" target_count="5" awards="10000|1,10000|2, 20000|3">
            <TargetCondition type="get_goods" item_id="3001" />
        </Task>
        <Task id="1002" title="累计获得[target_count]个[item_id]" target_count="5" awards="20000|1">
            <TargetCondition type="get_goods" item_id="3002" />
        </Task>
        <Task id="1003" title="累计获得[target_count]个[item_id]" target_count="5" awards="10000|1">
            <TargetCondition type="get_goods" item_id="3003" />
        </Task>
        <Task id="1004" title="累计获得[target_count]个[item_id]" target_count="5" awards="10000|1">
            <TargetCondition type="get_goods" item_id="3004" />
        </Task>
        <Task id="1005" title="累计获得[target_count]个[item_id]" target_count="5" awards="10000|1">
            <TargetCondition type="get_goods" item_id="3005" />
        </Task>  
</Tasks>


需要解析出各个子节点 Task 中的 id 和 awards 的值


源码如下


#include <iostream>
#include <boost/property_tree/xml_parser.hpp>

using namespace std;  
using namespace boost;
using namespace boost::property_tree;


int main( int argc, char* argv[] )  
{  
    ptree pt_empty;
    ptree pt;
    xml_parser::read_xml( "TaskCfg.xml", pt );
    try
    {
         boost::property_tree::ptree root = pt.get_child( "Tasks" );
         ptree pNode_EverydayTasks = pt.get_child( "Tasks.EverydayTasks" );    

         // 遍历 Task 节点
         for( ptree::iterator itr = pNode_EverydayTasks.begin(); itr != pNode_EverydayTasks.end(); itr++ )
         {            
             const ptree & attributes = itr->second.get_child( "<xmlattr>", pt_empty );    
            
             for ( ptree::const_iterator itr_attri = attributes.begin(); itr_attri!=attributes.end(); itr_attri++ )
             {
                 if ( "id" == std::string( itr_attri->first.data() ) )
                 {
                     cout << " id: " << itr_attri->second.data() << '\t';
                 }

                 if ( "awards" == std::string( itr_attri->first.data() ) )
                 {
                     cout << "awards: " << itr_attri->second.data() << endl;
                 }                
             }

             std::cout << std::endl;
         }
        
    }
    catch( std::exception& e )
    {
        std::cout << e.what() << std::endl;
    }

    return 0;  
}


注意上面的红色代码行,  必须把 ptree pt_empty; 作为第二个参数, 否则就不能正确解析;比如写成下面这样就解析不出来了

const ptree & attributes = itr->second.get_child( "<xmlattr>" );


boost 源码读起来太恶心了, 路过的朋友要是谁知道原理麻烦告诉我一下

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值