rapidxml遍历子节点例子

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <luaword>
 3     <btn>
 4         <info>
 5             <type>btn_click</type>
 6             <funinfo>BtnClick(%s)</funinfo>
 7             <name>按下按钮,%s</name>
 8         </info>
 9 
10         <info>
11             <type>btn_name</type>
12             <funinfo>GetBtnName(%s)</funinfo>
13             <name>获取按钮名字,%s</name>
14         </info>
15      </btn>
16 
17      <process>
18          <info>
19              <type>process_start</type>
20              <funinfo>StartProcess(%s,%s)</funinfo>
21              <name>启动进程,%s,%s</name>
22          </info>
23      </process>
24 
25      <edit>
26          <info>
27              <type>edit_set</type>
28              <funinfo>SetEditnam(%s,%s)</funinfo>
29              <name>设置文本框内容,%s</name>
30          </info>
31      </edit>
32 
33      <ctrl>
34          <info>
35              <type>edit_set</type>
36              <funinfo>SetEditnam(%s,%s)</funinfo>
37              <name>设置文本框内容,%s</name>
38          </info>
39      </ctrl>
40 
41      <wnd>
42          <info>
43              <type>fetch_wnd</type>
44              <funinfo>GetWnd(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)</funinfo>
45              <name>获取窗口,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s</name>
46          </info>
47      </wnd>
48  </luaword>

源xml

 1 // rapidxmlTest.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <rapidxml.hpp>
 7 #include <rapidxml_print.hpp>
 8 #include <rapidxml_utils.hpp>
 9 #include <process.h>
10 #include "CodingConvert.h"
11 int main()
12 {
13     rapidxml::file<> fdoc("luaword.xml");
14     std::string strbuf;
15 //    std::cout << CCodingConvert::Utf8_To_Gbk(fdoc.data()) << std::endl;
16     strbuf = CCodingConvert::Utf8_To_Gbk(fdoc.data());
17     rapidxml::xml_document<> doc;
18     doc.parse<0>(const_cast<CHAR*>(strbuf.data()));    //解析xml文档
19     //std::cout << doc.name() << doc.value() << std::endl;
20     //获取root 节点
21     rapidxml::xml_node<>* root = doc.first_node();
22     //std::cout << root->name() << std::endl;
23     rapidxml::xml_node<>* itemTmp = root->first_node();
24     std::string itemName = root->name();
25     for (;nullptr!=itemTmp;itemTmp = itemTmp->next_sibling())
26     {
27         //std::cout << itemTmp->name() << itemTmp->value() << std::endl;
28         rapidxml::xml_node<>* subItem = itemTmp->first_node();
29         for (;nullptr!=subItem;subItem=subItem->next_sibling())
30         {
31             auto type = subItem->first_node("type");
32             if (type)
33                 std::cout << type->name()<<":"<< type->value() << std::endl;
34             auto func = subItem->first_node("funinfo");
35             if (func)
36                 std::cout << func->name() << ":" << func->value() << std::endl;
37             auto name = subItem->first_node("name");
38             if (name)
39                 std::cout << name->name() << ":" << name->value() << std::endl;
40         }
41     }
42     system("pause");
43     return 0;
44 }

 

posted @ 2018-04-09 15:54  轻风々  阅读(698)  评论(0编辑  收藏  举报