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

article/2025/9/13 8:33:12

官方地址: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);


http://chatgpt.dhexx.cn/article/eqhg3yHo.shtml

相关文章

Ubuntu 18.04 LDAP认证

将ubuntu配置为通过ldap认证&#xff0c;使其成为ldap client&#xff0c;系统版本ubuntu 18.04。 一 软件安装 apt-get install ldap-utils libpam-ldap libnss-ldap nslcd配置1 配置2 配置3 配置4 配置5 配置6 配置7 配置8 配置8 二 认证方式中添加Ldap #auth-client-conf…

LDAP认证服务器

1.要准备的环境与软件(这里测试环境是Centos6.0-64位系统) alfresco-community-4.2.c-installer-linux-x64.bin (注: alfresco是一个免费开源系统&#xff0c;可以自己去下载) apache-tomcat-7.0.42.tar db-4.5.20.tar jdk-6u45-linux-x64.bin openldap-stable-20100219.tar ph…

Jumpserver部署+Ldap认证

内容导航 &#xff08;一&#xff09;jumpserver快速部署1&#xff0c;部署内容2&#xff0c;附上安装脚本3&#xff0c;解决github无法连接4&#xff0c;修改配置 &#xff08;二&#xff09;使用jumpserver1&#xff0c;登录信息2&#xff0c;添加主机3&#xff0c;web终端登…

SVN使用LDAP认证

前言 SVN架构 用户访问SVN服务器分为两个部分&#xff1a;认证与授权。 SVN内置了有认证与授权机制&#xff0c;其认证是通过SVN仓库内的passwd文件提供&#xff0c;但它是明文、静态的&#xff0c;不方便且安全性低。 SVN还支持外部的认证&#xff0c;比如SASL&#xff0c;…

ldap 认证 java_Java实现LDAP认证(上)

Baidu脑残&#xff0c;把原来的空间改得不伦不类。所以把一些技术的东西挪到这里。 我找到两种方法&#xff0c;大同小异&#xff0c;第一种是通过Spring&#xff0c;适合已经采用Spring的项目。 一般来说用户名和密码都是保存在数据库中。现在有这个需求&#xff0c;用户名和密…

Harbor 整合ldap认证

前提&#xff1a; ldap服务器已经安装&#xff1a;OpenLDAP安装部署 harbor服务器已经安装&#xff1a;Harbro v1.8.0部署 一、ldap组织结构 1、登录信息 2、查看用户信息 二、harbor配置 1、使用默认密码登录&#xff0c;admin/Harbor12345 2、认证模式 3、测试ldap服务器…

ldap认证 java_Java实现LDAP认证(上) | 学步园

Baidu脑残&#xff0c;把原来的空间改得不伦不类。所以把一些技术的东西挪到这里。 我找到两种方法&#xff0c;大同小异&#xff0c;第一种是通过Spring&#xff0c;适合已经采用Spring的项目。 一般来说用户名和密码都是保存在数据库中。现在有这个需求&#xff0c;用户名和密…

Zabbix 整合ldap认证

前提&#xff1a; zabbix部署完成&#xff1a;CentOS7.3 64位&#xff0c;搭建Zabbix3.4 ldap部署完成&#xff1a;OpenLDAP安装部署 一、LDAP服务端 1、ldap登录信息 2、查看ldap组织架构 3、添加zabbix默认用户Admin 二、Zabbix网页端 1、使用zabbix默认管理员用户登录 …

linux+配置ldap认证,Linux LDAP 认证配置

Linux通过LDAP方式&#xff0c;使用windows AD帐户登录linux shell&#xff0c;这个想法很cool吧。之前配置过一次&#xff0c;但过了太久忘记了&#xff0c;因此&#xff0c;今天把配 Linux通过LDAP方式&#xff0c;使用windows AD帐户登录linux shell&#xff0c;这个想法很c…

JumpServer 整合ldap认证

前提&#xff1a; ldap服务器已经安装&#xff1a;OpenLDAP安装部署 一、JumpServer安装 官网安装地址安装部署 - JumpServer 文档 1、一键部署 #系统版本 [rootlocalhost ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) #默认会安装到 /opt/jumpser…

linux samba 配置ldap认证,Samba集成Ldap认证

Samba集成Ldap认证 1.基础安装 yum -y install samba-common samba samba-client smbldap-tools openldap-clients nss-pam-ldapd 2.配置authconfig-tui 执行命令 "authconfig-tui" 验证配置 # getent passwd zhangsan:x:6460:18650:zhangsan:/home/zhangsan:/bin/…

SVN集成LDAP认证

如何将 LDAP 的认证&#xff0c;集成到 SVN 中。集成的办法&#xff0c;目前是有两种&#xff1a;一种是 SVN 直接通过 SVN 端口直接访问的&#xff0c;通过 SASL 实现 LDAP 的认证&#xff1b;另一种是 SVN 通过 Apache 进行 HTTP 访问的用户&#xff0c;通过配置 Apache &…

zabbix配置ldap认证

zabbix配置ldap认证 环境&#xff1a; centos6.6 zabbix3.0.3 域控服务器&#xff1a;windows-active server 2008 需求&#xff1a; 公司越来越大&#xff0c;人越来越多&#xff0c;配置人员的账号密码很麻烦。 为了集中管理&#xff0c;整合公司的用户密码&#xff0c…

LDAP认证-ldap介绍

OpenLDAP简介 LDAP 全称轻量级目录访问协议&#xff0c;是一个运行在 TCP/IP 上的目录访问协议。LDAP实现 提供被称为目录服务的信息服务&#xff0c;可以看做是一张特殊的数据库系统。可以有效的 解决众多网络服务的用户账户问题&#xff0c;规定了统一的身份信息数据库、身份…

LDAP认证

注&#xff1a;本文由网络公开资料整理而来&#xff0c;如有错误&#xff0c;欢迎指正。 LDAP&#xff08;Lightweight Directory Access Protocol&#xff09;是目录服务&#xff08;DAP&#xff09;在TCP/IP上的实现&#xff0c;它是对X.500目录协议的移植&#xff0c;但是简…

BlazeDS简单介绍

BlazeDS 是一个基于服务器的 Java 远程控制 (remoting) 和 Web 消息传递 (messaging) 技术&#xff0c;以LGPL&#xff08;Lesser GNU Public License&#xff09;公共许可证书发布。它能够使得后端的 Java 应用程序和运行在浏览器上的 Adobe Flex 应用程序相互通信。在Java应用…

关于BLAS的简单介绍

BLAS(Basic Linear Algebra Subprograms基础线性代数程序集)是进行向量和矩阵等基本线性代数操作的事实上的数值库。这些程序最早在1979年发布&#xff0c;是LAPACK(Linear Algebra PACKage)的一部分&#xff0c;便于建立功能更强的数值程序包。BLAS库在高性能计算中被广泛应用…

Blazor 基础入门

Blazor 基础知识 Intro Blazor 是微软在 .NET 里推出的一个 WEB 客户端 UI 交互的框架&#xff0c; 使用 Blazor 你可以代替 JavaScript 来实现自己的页面交互逻辑&#xff0c;可以很大程度上进行 C# 代码的复用&#xff0c;Blazor 对于 .NET 开发人员来说是一个不错的选择。 托…

Blazeds学习

BlazeDS是一个基于服务器的Java远程调用&#xff08;remoting&#xff09;和Web消息传递&#xff08;messaging&#xff09;技术&#xff0c;使得后台的Java应用程序和运行在浏览器上的Flex应用程序能够相互通信 一个BlazeDS应用包括两个部分&#xff1a;一个客户端应用程序和…

Blazeds初步

客户端应用 Blazeds包括客户端和服务端应用。客户端应用是典型的Adobe flex或者AIR应用。Flex和AIR应用使用flex组件和blazeds服务通信&#xff0c;包括Remote Object、HTTPService、WebService、Produce和Consumer。其中的HTTPService、WebService、Produce和Consumer是Flex S…