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

c++开源库rapidxml介绍与示例官方地址:http://rapidxml.sourceforge.net/官方手册:http://rapidxml.sourceforge.net/manual.html也可以在github上下载到别人上传的rapidxml:https://github.com/dwd/rapidxml1.头文件一般我们用到的头文件只有这三个#include”rapidxml/rapidxml.hpp”

大家好,又见面了,我是你们的朋友全栈君。

官方地址: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[])
{

    //读取xml
    rapidxml::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[])
{

    //读取xml
    rapidxml::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 char
    doc.parse<0>(fdoc.data());// 0 means default parse flags
    std::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[])
{

    //读取xml
    rapidxml::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 char
    doc.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[])
{

    //读取xml
    rapidxml::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 char
    doc.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);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160916.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • RangeValidator1 日期验证格式

    RangeValidator1 日期验证格式13.3验证控件的类型到目前为止,已经讨论了验证的相关理论。ASP.NET2.0提供了5种验证控件,表13-1对此进行了描述。然后,将介绍每种控件的细节,首先是表格式概述。13.3.1类型表表13-1控件名适用情况RequiredFieldValidator为了避免空值,例如当用户输入密码以建立新账户时…

    2022年7月12日
    18
  • 【一键新机】免root/不刷机/拒绝Xposed 实现 Android改机,全新技术分析。「建议收藏」

    【一键新机】免root/不刷机/拒绝Xposed 实现 Android改机,全新技术分析。「建议收藏」此篇文章仅探讨Android底层技术。不提供任何软件和安装包……我们通过一些底层技术对手机中的各种基础信息进行一个修改。主要修改的参数有:Android_Id、IMEI、手机序列号、手机号码、WIFI-MAC、WIFI-Name、安卓系统版本、ROM固件、手机号码、手机型号、手机品牌、CPU型号、手机制造商、GPS定位、通话记录模拟、手机短信模拟……等各类安卓手机系统信息的修改功能…

    2022年6月8日
    138
  • iOS 二级菜单(UITableView实现)「建议收藏」

    iOS 二级菜单(UITableView实现)「建议收藏」iOS二级菜单(UITableView实现)注释全帮助新手学习

    2022年6月8日
    37
  • 如何部署服务器虚拟化,vmware服务器虚拟化方案(vmware虚拟化平台部署)

    如何部署服务器虚拟化,vmware服务器虚拟化方案(vmware虚拟化平台部署)服务器虚拟化平台方案主要的有三种 特点分别如下 1 思杰 CitrixXenSer XenCenter 是 Citrix 的虚拟化图形接口管理工具 可在同一界面 管理多台的 XenServer 服务 以前见过一台服务器安装虚拟服务器 然后可以装 N 个系统 又节约硬件 又 1VMwareESXSe 的安装 VMwareESXSer 需要 2GB 的内存 在开始测试的时候 为 ESXSer

    2025年9月5日
    2
  • 谱图理论(spectral graph theory)

    谱图理论(spectral graph theory)介绍如何理解特征值和特征向量此部分参考了马同学的文章:如何理解矩阵特征值和特征向量?我们知道一个矩阵可以看做是线性变换又或者是某种运动,可以将一个向量进行旋转,平移等等操作,正常来说,对于一个向量vvv,并对其乘上一个A会出现下图的情况:可以看到乘了A之后v发生了一些旋转。然而所有向量中存在一种稳定的向量,他不会发生旋转,平移,只会使得向量变长或变短,而这种稳定的向量正是矩阵的特征向…

    2025年6月6日
    1
  • 从程序员到项目经理 技术荒废了_it售前工程师

    从程序员到项目经理 技术荒废了_it售前工程师从程序员到项目经理(一):为什么要当项目经理“从程序员到项目经理”,这个标题让我想起了很久以前一本书的名字《从Javascript到Java》。然而,从Javascript到Java充其量只是工具的更新,而从程序员到项目经理,却是一个脱胎换骨的过程。从Javascript到Java,是一个取巧的方法;而从程序员到项目经理,却并无捷径可走,必须从内而外的改变和提升。一.为什么要当项目经理

    2025年11月2日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号