日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++开源库rapidxml介绍与示例

發布時間:2024/8/1 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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::print

2.常用方法:

1)加載一個XML文件的內容

方法:rapidxml::file<> valName(“filepath”);
定義:rapildxml_print_utils.hpp,這個頭文件中定義了file類,這個類有兩個成員函數data()和size()分別返回char*的xml文本內容和unsigned int型的文本數據長度
示例:

#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");std::cout<<"************************powered by rapidxml**********************"<<std::endl;std::cout<< fdoc.data()<< std::endl;std::cout<<"length of xml:"<<fdoc.size()<<std::endl;std::cout<<"******************************************************************"<<std::endl;return 0; }

運行一下!

2)加載DOM tree

類:xml_document
定義一個該類的對象doc
rapidxml::xml_document<> doc;
類的定義位置:rapidxml.hpp
類的成員函數:
1)parse(Ch *text) 將數據解析為DOM Tree
使用時doc.parse(text);
parseFlag指定格式,可以用’|’來組合使用
常用的parseFlag:
parseFlag為0表示默認的parseflag
parse_comment_nodes表示帶上xml中的注釋
parse_no_data_nodes在要修改結點值的時候要設置這個parseFlag
2) clear() 清空DOM Tree
此外xml_document繼承自xml_node因此還具有xml_node的方法。
示例:

#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");std::cout<<"************************powered by rapidxml**********************"<<std::endl;std::cout<< fdoc.data()<< std::endl;std::cout<<"length of xml:"<<fdoc.size()<<std::endl;std::cout<<"******************************************************************"<<std::endl;rapidxml::xml_document<> doc;// character type defaults to chardoc.parse<0>(fdoc.data());// 0 means default parse flagsstd::cout<<"#1"<<std::endl<<doc<<std::endl;doc.clear();std::cout<<"#2"<<std::endl<<doc<<std::endl;return 0; }

運行一下!

3)獲取DOM Tree結點

rapidxml::xml_node<> *root;
類:xml_node
定義一個該類的對象root
定義于:rapidxml.hpp
常用的類成員函數:
1)node_type type() const; 獲取結點類型 獲取的類型是枚舉的
2)Ch* name() const; 獲取結點名
3)std::size_t name_size() const; 獲取結點名長度
4)Ch* value() const; 獲取結點值
5)std::size_t value_size() const; 獲取結點值長度
6)xml_node* first_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取DOM Tree第一個子結點的指針
第一個參數為節點名,如果給定第一個參數為”a”,則該函數尋找結點名為a的第一個子結點;第二個參數為結點名長度
7)xml_node* last_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取DOM Tree最后一個子結點的指針
參數含義同上
8)xml_attribute* first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取結點的第一個屬性指針
9)xml_attribute* next_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取結點的下一個屬性指針
10)xml_attribute* last_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const;獲取結點的最后一個屬性指針
屬性指針的格式見類xml_attribute
11)xml_node* previous_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const;獲取上一個同級結點的指針
12)xml_node* next_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取下一個同級結點的指針
13)xml_attribute* first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取第一個同級結點的指針
14)xml_attribute* last_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const; 獲取最后一個同級結點的指針
15)void insert_node(xml_node< Ch > *where, xml_node< Ch > *child);在第一個參數指向的結點之前,插入一個結點

示例:
test.xml

<?xml version="1.0" encoding="utf-8" standalone="no"?> <!--This is a test xml file--> <note><to gender="male" id = "1">George</to><from>John</from><heading>Reminder</heading><body>Don't forget the meeting</body> </note>

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的第一個子結點就是根結點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)操作符<<
示例:

#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;rapidxml::xml_document<> doc; // character type defaults to chardoc.parse<rapidxml::parse_no_data_nodes|rapidxml::parse_comment_nodes>(fdoc.data()); // 0 means default parse flags//DOM Tree的第一個子結點就是根結點rapidxml::xml_node<> *root = doc.first_node("note");rapidxml::xml_node<> *node_first = root->first_node();cout<<"first node of root:"<<endl<<*node_first<<endl;node_first->value("xchen");cout<<"******************************************************************"<<endl;cout<<doc<<endl;ofstream out("conver/after.xml");out << doc;out.close();return 0; }

運行一下!

6.為一個新的屬性或者結點分配空間

1)為結點分配空間
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);

總結

以上是生活随笔為你收集整理的c++开源库rapidxml介绍与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。