(4)rapidxml的详解及使用
RapidXml是指 XML DOM解析工具包,是一个快速的读写xml文件的库文件(hpp)。
(1)创建XML文件
#include <iostream>
#include <string>
#include <fstream>
#include "string.h"
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
#include "rapidxml_utils.hpp" static const int buf_len = ;
static char buf[buf_len] = { }; void createXML(const char * file_name)
{
// 1.DOM
rapidxml::xml_document<> doc; // 2.node_declaration
rapidxml::xml_node<>* declaration = doc.allocate_node(rapidxml::node_declaration);
declaration->append_attribute(doc.allocate_attribute("version", "1.0"));
declaration->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
doc.append_node(declaration); // 3.node_pi
rapidxml::xml_node<>* dec = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version=\"1.0\" encoding=\"utf-8\""));
doc.append_node(dec); // 4.node_element
rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");
doc.append_node(root); // 5.node_comment
rapidxml::xml_node<>* comment = doc.allocate_node(rapidxml::node_comment, , "这是一个注释节点");
root->append_node(comment); rapidxml::xml_node<>* students = doc.allocate_node(rapidxml::node_element, "students"); // 6.node_data
rapidxml::xml_node<>* one_student = doc.allocate_node(rapidxml::node_element, "student");
rapidxml::xml_node<>* name = doc.allocate_node(rapidxml::node_data,"node_name","");
one_student->append_node(name);
students->append_node(one_student); // 7.node_element with value
rapidxml::xml_node<>* two_student = doc.allocate_node(rapidxml::node_element, "student","");
students->append_node(two_student); // 8.set attribute
rapidxml::xml_node<>* three_student = doc.allocate_node(rapidxml::node_element,"student","");
students->append_node(three_student);
three_student->append_attribute(doc.allocate_attribute("course", doc.allocate_string(buf)));
three_student->append_attribute(doc.allocate_attribute("score","")); // 9.node_element without value
rapidxml::xml_node<>* four_student = doc.allocate_node(rapidxml::node_element,"student");
students->append_node(four_student); // 10.node_cdata
rapidxml::xml_node<>* five_student = doc.allocate_node(rapidxml::node_cdata,"student","");
students->append_node(five_student); // 11.node_cdata
rapidxml::xml_node<>* six_student = doc.allocate_node(rapidxml::node_pi,"student","");
students->append_node(six_student); // 12.node_cdata
rapidxml::xml_node<>* seven_student = doc.allocate_node(rapidxml::node_doctype,"student","");
students->append_node(seven_student);
root->append_node(students); // 13.输出DOM到命令行
std::cout<<doc; // 14.输出DOM到文件
std::ofstream outfile(file_name, std::ios::out);
if (outfile)
{
char *end = rapidxml::print(buf, doc, );
*end = ;
outfile << buf;
outfile.close();
}
} int main()
{
const char *file_name = "rapid.xml";
createXML(file_name);
return ;
}
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<root>
<!--这是一个注释节点-->
<students>
<student></student>
<student></student>
<student course="" score=""></student>
<student/>
<![CDATA[]]>
<?student ?>
<!DOCTYPE >
</students>
</root>
(2)读取XML文件
//rapidxml_utils.hpp
file类
data() 返回 char* 的xml文本内容
size() 返回 unsigned int的文本数据长度
定义:rapidxml::file<0> valName(“filepath”);
//rapidxml.hpp
xml_document类
parse(Ch *text);将文本数据解析为DOM tree
clear();清空DOM tree
定义:rapidxml::xml_document<0> doc;
const int parse_default = 0;
const int parse_full = parse_declaration_node | parse_comment_nodes | parse_doctype_node | parse_pi_nodes | parse_validate_closing_tags
#include <iostream>
#include <string>
#include <fstream>
#include "unistd.h"
#include "string.h"
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
#include "rapidxml_utils.hpp" static const int buf_len = ;
static char buf[buf_len] = { }; //利用rapidxml::file读取配置文件
void readXMLByFile(const char *file_name)
{
try
{
// 1.清空缓冲区
memset(buf,,buf_len);
// 2.拼接绝对路径
std::string strXml = "/home/sunjimeng/test/";
strXml.append(file_name);
// 3.用file文件读入缓冲区
rapidxml::file<> fdoc(strXml.c_str());
// 4.打印从文件中读取的内容
std::cout<<fdoc.data();
// 5.解析获取DOM实例
rapidxml::xml_document<> doc;
doc.parse<>(fdoc.data());
}catch(rapidxml::parse_error e)
{
std::cout<<e.what()<<std::endl;
return;
}
}
void readXMLByStream(const char *file_name)
{
try
{
// 1.清空缓冲区
memset(buf,,buf_len);
// 2.判断文件是否存在
if(access(file_name,F_OK) == -)
std::cout<<"xml file "<<file_name<<"is not exits!"<<std::endl;
// 3.实例化文件读取流
std::ifstream infile(file_name, std::ios::in);
if(!infile)
{
std::cout<<"file stream instance error!"<<std::endl;
return;
}
// 4.读取文件内容到缓冲区
infile.read(buf, buf_len);
// 5.输出文件内容
std::cout<<buf<<std::endl;
// 6.实例化DOM
rapidxml::xml_document<> doc;
doc.parse<>(buf);
}catch(rapidxml::parse_error e)
{
std::cout<<e.what()<<std::endl;
return;
}
}
int main()
{
const char *file_name = "rapid.xml";
readXMLByFile(file_name);
readXMLByStream(file_name);
return ;
}
(3)修改及删除(接着上文)
//rapidxml.hpp
xml_node类
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;获取结点的最后一个属性指针
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);在第一个参数指向的结点之前,插入一个结点
xml_attribute类
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;获取后一个属性
#include <iostream>
#include <string>
#include <fstream>
#include "unistd.h"
#include "string.h"
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
#include "rapidxml_utils.hpp" static const int buf_len = ;
static char buf[buf_len] = { }; void parseXML(const char * file_name)
{
memset(buf,,buf_len);
try
{
std::ifstream infile(file_name, std::ios::in);
if (!infile)
{
return;
}
infile.read(buf, buf_len);
std::cout << buf << std::endl; rapidxml::xml_document<> doc;
doc.parse<>(buf);
// 取得根节点
rapidxml::xml_node<> *root = doc.first_node("root");
// 遍历students的子节点
int flag = ;
for (rapidxml::xml_node<> * node = root->first_node("students")->first_node(); node; node = node->next_sibling())
{
if(node->first_node() != NULL)
std::cout<<"name :"<<node->name()<<" value : "<<node->value()<<std::endl;
else
std::cout<<"name :"<<node->name()<<" has no value!"<<std::endl;
for(rapidxml::xml_attribute<> * attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute())
std::cout<<" attribute name = "<<attribute->name()<<" attribute value = "<<attribute->value()<<std::endl;
}
} catch(rapidxml::parse_error e)
{
std::cout<<e.what()<<std::endl;
}
}
void modifyXML(const char * file_name)
{
memset(buf,,buf_len);
//用file解析DOM时必须是绝对路径
std::string strXml = "/home/sunjimeng/test/";
strXml.append(file_name);
rapidxml::file<> fdoc(strXml.c_str());
//打印读取的文件
//std::cout<<fdoc.data();
rapidxml::xml_document<> doc;
doc.parse<>(fdoc.data());
//取得根节点
rapidxml::xml_node<> *root = doc.first_node("root");
rapidxml::xml_node<> *students = root->first_node("students");
if(students != NULL)
std::cout<<"student is not null"<<std::endl;
else
return;
//删除最后一个元素
if(students->last_node() != NULL)
students->remove_last_node();
//删除第一个元素
if(students->first_node() != NULL)
students->remove_first_node();
rapidxml::xml_node<> *ptrNode = students->first_node(); int size = ;
while(ptrNode)
{
size++;
//删除所有属性
ptrNode->remove_all_attributes();
ptrNode = ptrNode->next_sibling();
}
std::cout<<"size = "<<size<<std::endl;
std::string text;
rapidxml::print(std::back_inserter(text),doc,);
std::cout<<text<<std::endl;
std::ofstream out(file_name);
out<<doc;
}
int main()
{
const char *file_name = "rapid.xml";
modifyXML(file_name);
parseXML(file_name);
return ;
}
student is not null
size =
<root>
<students>
<student></student>
</students>
</root> <root>
<students>
<student></student>
</students>
</root> name :student value :
(4)rapidxml的详解及使用的更多相关文章
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- Git初探--笔记整理和Git命令详解
几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...
- Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)
Android XML shape 标签使用详解 一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...
- Node.js npm 详解
一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...
随机推荐
- Linux FTP 命令全集
Linux FTP 命令全集 1 前言 下面就所有命令给出解释和例子. 说明: 1. remote-file 指远程文件,即服务器上的文件 2. local-file 指本地文件,即本地机器上的文 ...
- java 权限控制
网上或参考书中,对于java权限控制大多给出一张看似很整齐很好记实则不好理解的表格,我整理了一个2.0升级版,自认为会好理解很多,希望可以有所帮助. 同一包内 不同包内 修饰符 当前类 非当前类(含子 ...
- Satellite-Hacking 攻击卫星/卫星安全
虽说卫星安全这种东西也是高富帅才玩得起的领域,但是了解了解总是没坏处.参考了一些资料,如果想详细了解可以戳进去看看.看了这么多资料,总结一下吧. Why? 卫星存在安全问题主要有一下俩原因,首先是成本 ...
- PAT乙级1018
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805304020025344 题解 刚开始做很懵逼,可能并不难吧 ...
- 查看ocx控件CLSID的方法(转载)
CLSID就是classID类的标识码 1.打开注册表,window + r ,输入regedit,确定 2.点击 编辑 选择查找 3.ok拉 参考:https://blog.csdn.net/u01 ...
- C++ SQLite的使用总结
SQLite3简介 SQLite3只是一个轻型的嵌入式数据库引擎,占用资源非常低,处理速度比Mysql还快,专门用于移动设备上进行适量的数据存取,它只是一个文件,不需要服务器进程. SQL语句是SQL ...
- C# class 浅拷贝 与 深拷贝
MemberwiseClone 方法创建一个浅表副本,具体来说就是创建一个新对象,然后将当前对象的非静态字段复制到该新对象.如果字段是值类型的,则对该字段执行逐位复制.如果字段是引用类型,则复制引用但 ...
- es实战之数据导出成csv文件
从es将数据导出分两步: 查询大量数据 将数据生成文件并下载 本篇主要是将第二步,第一步在<es实战之查询大量数据>中已讲述. csv vs excel excel2003不能超过6553 ...
- PPM / PGM / PBM 图像文件格式[转]
下面将详细介绍ppm文件 ppm文件是一种图像文件,有其自己的文件格式.ppm文件由两个部分组成:第一个部分是三行ASCII码,这个部分决定了图像的存储格式以及图像的特征:第二个部分就是图像的数据部分 ...
- JProfiler集成在eclipse中(转)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/sinat_38259539/articl ...