官方地址: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
// using namespace std;
int main(int argc, char const *argv[])
{
//读取xml
rapidxml::file<> fdoc("test.xml");
std::cout<<"************************powered by rapidxml**********************"<
std::cout<< fdoc.data()<< std::endl;
std::cout<<"length of xml:"<
std::cout<<"******************************************************************"<
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
// using namespace std;
int main(int argc, char const *argv[])
{
//读取xml
rapidxml::file<> fdoc("test.xml");
std::cout<<"************************powered by rapidxml**********************"<
std::cout<< fdoc.data()<< std::endl;
std::cout<<"length of xml:"<
std::cout<<"******************************************************************"<
rapidxml::xml_document<> doc;// character type defaults to char
doc.parse<0>(fdoc.data());// 0 means default parse flags
std::cout<<"#1"<
doc.clear();
std::cout<<"#2"<
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
George
John
Reminder
Don't forget the meeting
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/115978.html