xmlcpp支持便捷的遍历搜索,算法高效稳健。可遍历搜索节点name,节点属性key,节点属性value,以及节点文本text。除key搜索需完全匹配外,其余搜索均只需提供被搜索内容的某个连续字段。
方法列表:
    - find_has_name
 
    - find_has_key
 
    - find_has_value
 
    - find_has_text
 
以下一个简单的例子。
首先生成一个内容如下的xml文件:
	  
		|  代码如下 | 
		复制代码 | 
	  
	  
		| 
  
      
         数据结构 
         严蔚敏 
         30 
      
      
         路由器与交换型互联网基础 
         程庆辉 
         27 
      
      
         计算机硬件技术基础 
         李继灿 
         25 
      
      
         软件质量保证与管理 
         朱少民 
         39 
      
      
         算法设计与分析 
         王红梅 
         23 
      
      
         计算机操作系统 
         7-111-19149-1 
         28 
      
  
 | 
	  
	
演示代码如下:
	  
		|  代码如下 | 
		复制代码 | 
	  
	  
		| 
 //============================================================================ 
 // Name        : libxmlcpp_test.cpp 
 // Author      : [email protected],[email protected] 
 // Version     : 1.0 
 //============================================================================ 
  
 #include "xmlcpp.h" 
  
 using namespace std; 
 using namespace xmlcpp; 
  
 int main() { 
  
     string title_text[] = { "数据结构", "路由器与交换型互联网基础", "计算机硬件技术基础", "软件质量保证与管理", 
             "算法设计与分析", "计算机操作系统" }; 
     double price_text[] = { 30, 27, 25, 39, 23, 28 }; 
     string author_text[] = 
             { "严蔚敏", "程庆辉", "李继灿", "朱少民", "王红梅", "7-111-19149-1" }; 
  
     node bookstore("bookstore"); 
  
     for (int i(1); i < 7; ++i) { 
  
         node book("book"); 
         book["Type"] = "必修课"; 
         if (i != 6) { 
             book["ISBN"] = string("7-111-19149-") + num2str(i + 1); 
         } else { 
             book["ISBN"] = string("7-111-19149-") + num2str(1); 
         } 
  
         node title("title"); 
         node author("author"); 
         node price("price"); 
  
         title.set_text(title_text[i - 1]); 
         author.set_text(author_text[i - 1]); 
         price.set_text(num2str(price_text[i - 1])); 
  
         book.push_back(title); 
         book.push_back(author); 
         book.push_back(price); 
  
         bookstore.push_back(book); 
  
     } 
  
     string file = bookstore.get_name() + ".xml"; 
  
     bookstore.save(file); 
  
     node test; 
     test.load(file); 
  
     deque &tmp = test.find_has_text("计算"); 
     for (size_t i = 0; i < tmp.size(); ++i) { 
         cout << *tmp[i] << endl; 
     } 
  
     return 0; 
 } 
 | 
	  
	
结果
搜索结果:

ie9文件浏览:

eclipse xml浏览文件:

xmlcpp遍历搜索,就是这么简单!