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)
上一篇 2022年7月17日 上午6:36
下一篇 2022年7月17日 上午6:36


相关推荐

  • jQuery发送Ajax请求

    jQuery发送Ajax请求Ajax 用于浏览器与服务器通信而无需刷新整个页面 服务器将不再返回整个页面 而是返回少量数据 通过 JavaScriptDO 更新一部分节点 期间数据传输可采用 xml json 等格式 Ajax 最早用于谷歌的搜索提示 其实不刷新整个页面便可与服务器通信的方法有很多 比如 Flash Javaapplet iframe 等 但 Ajax 是目前最为常见的一种 我们可以使用 JavaScript 扩展对象 XMLHttpReque 实现 Ajax 对于这种方法在这里不做介绍 下面直接了解 jQuery 实现 Ajax 的几种

    2026年3月19日
    2
  • idea 在线激活码_在线激活

    (idea 在线激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html0VOERWDQ5R-eyJsaWNlbnNlSWQi…

    2022年3月31日
    90
  • C++ 字符串转时间 与 时间转转字符串[通俗易懂]

    C++ 字符串转时间 与 时间转转字符串[通俗易懂]1、常用的时间存储方式1)time_t类型,这本质上是一个长整数,表示从1970-01-0100:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。2)tm结构,这本质上是一个结构体,里面包含了各时间字段structtm{inttm_sec;/*secondsafterthe…

    2022年6月2日
    320
  • 有关软件设计师的报名问题及答案_软件设计师软考

    有关软件设计师的报名问题及答案_软件设计师软考下面是青岛人才市场的联系地址和电话 青岛市中高级人才市场海尔路178号889166728891667088916679 中国青岛人才大市场山东路171号856329738564389885636580 贵州路人才市场贵州路69号一楼咨询:82685619 城阳人才市场(人才交流中心)城阳正阳路222号87868756 胶南市人才交流服务中心新华路8

    2025年9月23日
    12
  • 编程干货│全网最全 adb 命令[通俗易懂]

    编程干货│全网最全 adb 命令[通俗易懂]adb命令是Android开发和测试人员不可替代的强大工具

    2022年7月27日
    9
  • ntp校时服务器(NTP校时器)介绍

    ntp校时服务器(NTP校时器)介绍酷鲨科技作为一家时间同步领域科研公司 今天简单介绍一下 ntp 校时服务器 其实大部分人认为 ntp 校时服务器是不同于 NTP 网络时间同步服务器 其实不然 基本是同一种产品 只是叫法不同 ntp 校时服务器页可以称为 NTP 校时器 ntp 校时服务器主要作用 ntp 校时服务器主要还是帮助不同网络设备之间进行时间同步 然而 同步 概念在通信领域中频率的同步 或者网络中每个节点的时钟频率与相位的同步 并且在规定的标准误差之间 其实网站时间的同步并没有解决 ntp 校时服务器的功能也就是让网络中的每一个时钟节点 频率 每一个具

    2026年3月16日
    2

发表回复

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

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