c++开源库rapidxml介绍与示例
官方地址:http://rapidxml.sourceforge.net/
官方手冊:http://rapidxml.sourceforge.net/manual.html
也可以在github上下載到別人上傳的rapidxml:https://github.com/dwd/rapidxml
1.頭文件
一般我們用到的頭文件只有這三個
#include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" //rapidxml::file #include "rapidxml/rapidxml_print.hpp" //rapidxml::print2.常用方法:
1)加載一個XML文件的內(nèi)容
方法:rapidxml::file<> valName(“filepath”);
定義:rapildxml_print_utils.hpp,這個頭文件中定義了file類,這個類有兩個成員函數(shù)data()和size()分別返回char*的xml文本內(nèi)容和unsigned int型的文本數(shù)據(jù)長度
示例:
運行一下!
2)加載DOM tree
類:xml_document
定義一個該類的對象doc
rapidxml::xml_document<> doc;
類的定義位置:rapidxml.hpp
類的成員函數(shù):
1)parse(Ch *text) 將數(shù)據(jù)解析為DOM Tree
使用時doc.parse(text);
parseFlag指定格式,可以用’|’來組合使用
常用的parseFlag:
parseFlag為0表示默認(rèn)的parseflag
parse_comment_nodes表示帶上xml中的注釋
parse_no_data_nodes在要修改結(jié)點值的時候要設(shè)置這個parseFlag
2) clear() 清空DOM Tree
此外xml_document繼承自xml_node因此還具有xml_node的方法。
示例:
運行一下!
3)獲取DOM Tree結(jié)點
rapidxml::xml_node<> *root;
類:xml_node
定義一個該類的對象root
定義于:rapidxml.hpp
常用的類成員函數(shù):
1)node_type type() const; 獲取結(jié)點類型 獲取的類型是枚舉的
2)Ch* name() const; 獲取結(jié)點名
3)std::size_t name_size() const; 獲取結(jié)點名長度
4)Ch* value() const; 獲取結(jié)點值
5)std::size_t value_size() const; 獲取結(jié)點值長度
6)xml_node* first_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取DOM Tree第一個子結(jié)點的指針
第一個參數(shù)為節(jié)點名,如果給定第一個參數(shù)為”a”,則該函數(shù)尋找結(jié)點名為a的第一個子結(jié)點;第二個參數(shù)為結(jié)點名長度
7)xml_node* last_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取DOM Tree最后一個子結(jié)點的指針
參數(shù)含義同上
8)xml_attribute* first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取結(jié)點的第一個屬性指針
9)xml_attribute* next_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取結(jié)點的下一個屬性指針
10)xml_attribute* last_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const;獲取結(jié)點的最后一個屬性指針
屬性指針的格式見類xml_attribute
11)xml_node* previous_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const;獲取上一個同級結(jié)點的指針
12)xml_node* next_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取下一個同級結(jié)點的指針
13)xml_attribute* first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取第一個同級結(jié)點的指針
14)xml_attribute* last_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取最后一個同級結(jié)點的指針
15)void insert_node(xml_node< Ch > *where, xml_node< Ch > *child);在第一個參數(shù)指向的結(jié)點之前,插入一個結(jié)點
示例:
test.xml
cpp
#include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" //rapidxml::file #include "rapidxml/rapidxml_print.hpp" //rapidxml::print#include <iostream> using namespace std; int main(int argc, char const *argv[]) {//讀取xmlrapidxml::file<> fdoc("test.xml");cout<<"************************powered by rapidxml**********************"<<endl;cout<< fdoc.data()<< endl;cout<<"length of xml:"<<fdoc.size()<<endl;cout<<"******************************************************************"<<endl;rapidxml::xml_document<> doc;// character type defaults to chardoc.parse<0>(fdoc.data());// 0 means default parse flags//DOM Tree的第一個子結(jié)點就是根結(jié)點rapidxml::xml_node<> *root = doc.first_node();cout<<"root:"<<root<<endl;cout<<*root<<endl;cout<<"root name:"<<root->name()<<endl;cout<<"root name_size:"<<root->name_size()<<endl;rapidxml::xml_node<> *node_first = root->first_node();cout<<"first node of root:"<<endl<<*node_first<<endl;rapidxml::xml_node<> *node_last = root->last_node();cout<<"last node of root:"<<endl<<*node_last<<endl;node_first = root->first_node("to");rapidxml::xml_attribute<> *attr; attr = node_first->first_attribute();cout<<"attr_name:"<<attr->name()<<endl;cout<<"attr_value:"<<attr->value()<<endl;attr = attr->next_attribute();cout<<"attr_name:"<<attr->name()<<endl;cout<<"attr_value:"<<attr->value()<<endl<<endl;for(;node_first!=NULL;node_first = node_first->next_sibling()){cout<<"sib:"<<*node_first;cout<<"sib name:"<<node_first->name()<<endl;cout<<"sib value:"<<node_first->value()<<endl<<endl;}// rapidxml::xml_node<> *node_last = doc.last_node();// std::cout<<node_last<<std::endl;// std::cout<<*node_first<<std::endl;return 0; }運行一下!
4.獲取屬性
類:xml_attribute
定義于:rapidxml.hpp
常用方法:
1)xml_attribute *previous_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const;獲取前一個屬性
2)xml_attribute *next_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const;獲取后一個屬性
5.輸出
1)操作符<<
示例:
運行一下!
6.為一個新的屬性或者結(jié)點分配空間
1)為結(jié)點分配空間
xml_node* allocate_node(node_type type, const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0);
2)為屬性分配空間
xml_attribute* allocate_attribute(const Ch *name=0, const Ch *value=0, std::size_t name_size=0, std::size_t value_size=0);
總結(jié)
以上是生活随笔為你收集整理的c++开源库rapidxml介绍与示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Socket编程实例解析
- 下一篇: Weblogic部署