使用rapidxml操作xml~讀寫文件操作

rapidxml~網上很容易下,下面介紹使用方法:

1、xml寫文件
如下:

#include <iostream>
#include 
<rapidxml/rapidxml.hpp>
#include 
<rapidxml/rapidxml_utils.hpp>
#include 
<rapidxml/rapidxml_print.hpp>

using namespace rapidxml;

int main()
{    
      xml_document
<> doc;  
      xml_node
<>* rot = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version='1.0' encoding='utf-8'"));
      doc.append_node(rot);
      xml_node
<>* node =   doc.allocate_node(node_element,"config","information");  
      xml_node
<>* color =   doc.allocate_node(node_element,"color",NULL);  
      doc.append_node(node);
      node
->append_node(color);
      color
->append_node(doc.allocate_node(node_element,"red","0.1"));
      color
->append_node(doc.allocate_node(node_element,"green","0.1"));
      color
->append_node(doc.allocate_node(node_element,"blue","0.1"));
      color
->append_node(doc.allocate_node(node_element,"alpha","1.0"));

      xml_node
<>* size =   doc.allocate_node(node_element,"size",NULL); 
      size
->append_node(doc.allocate_node(node_element,"x","640"));
      size
->append_node(doc.allocate_node(node_element,"y","480"));
      node
->append_node(size);

      xml_node
<>* mode = doc.allocate_node(rapidxml::node_element,"mode","screen mode");
      mode
->append_attribute(doc.allocate_attribute("fullscreen","false"));
      node
->append_node(mode);

      std::
string text;  
      rapidxml::print(std::back_inserter(text), doc, 
0);  

      std::cout
<<text<<std::endl; 
    
      std::ofstream 
out("config.xml");
     
out << doc;

      system(
"PAUSE");
     
return EXIT_SUCCESS;
}

生成的xml文件爲:
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客<?xml version='1.0' encoding='utf-8' ?>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
<config>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
<color>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<red>0.1</red>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<green>0.1</green>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<blue>0.1</blue>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<alpha>1.0</alpha>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
</color>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
<size>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<x>640</x>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<y>480</y>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
</size>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
<mode fullscreen="false">screen mode</mode>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
</config>

2、讀xml文件
基本的步驟爲
首先獲取xml文件數據
然後分析數據
獲取節點
獲取屬性
獲取名字
獲取值
...

代碼如下:
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客#include <iostream>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客#include 
<rapidxml/rapidxml.hpp>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客#include 
<rapidxml/rapidxml_utils.hpp>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客#include 
<rapidxml/rapidxml_print.hpp>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客 
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
using namespace rapidxml;
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
int main()
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
{    
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      file
<> fdoc("config.xml");
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      std::cout
<<fdoc.data()<<std::endl; 
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      xml_document
<>   doc;    
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      doc.parse
<0>(fdoc.data()); 
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      std::cout
<<doc.name()<<std::endl;
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客     
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客     
//! 獲取根節點
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
      xml_node<>* root = doc.first_node();
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      std::cout
<<root->name()<<std::endl;
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客     
//! 獲取根節點第一個節點
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
      xml_node<>* node1 = root->first_node();
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      std::cout
<<node1->name()<<std::endl; 
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      xml_node
<>* node11 = node1->first_node();
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      std::cout
<<node11->name()<<std::endl;
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      std::cout
<<node11->value()<<std::endl;
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客     
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客     
//! 修改之後再次保存
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
      xml_node<>* size = root->first_node("size");
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      size
->append_node(doc.allocate_node(node_element,"w","0"));
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      size
->append_node(doc.allocate_node(node_element,"h","0"));
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      std::
string text;  
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      rapidxml::print(std::back_inserter(text),doc,
0);  
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客 
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      std::cout
<<text<<std::endl; 
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      std::ofstream 
out("config.xml");
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客     
out << doc;
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客      system(
"PAUSE");
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客     
return EXIT_SUCCESS;
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客}

使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
生成的xml爲:
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客<?xml version='1.0' encoding='utf-8' ?>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
<config>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
<color>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<red>0.1</red>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<green>0.1</green>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<blue>0.1</blue>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<alpha>1.0</alpha>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
</color>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
<size>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<x>640</x>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客        
<y>480</y>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
</size>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客    
<mode fullscreen="false">screen mode</mode>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
</config>
使用rapidxml操作xml~讀寫文件操作 - songhao0201 - songhao0201的博客
需要說明的是rapidxml明顯有一個bug
那就是append_node(doc.allocate_node(node_element,"h","0"));的時候並不考慮該對象是否存在!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章