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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 如何关闭ESLint,一次成功

    如何关闭ESLint,一次成功ESLint可以用来识别ECMAScript,并且按照规则给出报告的代码检测工具,使用它可以避免低级错误和统一代码的风格。但是有时候新手会被ESLint的报错阻止程序的运行,这时候我们就想关闭这个ESLint了。vue项目中关闭ESLint方法:找到build文件夹—>webpack.base.conf.js—->module然后重启服务,npmrundev就可以…

    2022年5月5日
    243
  • 自动化测试之超厉害自动化录制工具介绍

    自动化测试之超厉害自动化录制工具介绍

    2021年9月17日
    149
  • pycharm替换的快捷键_想要快速编写代码,你得熟悉这些快捷键!

    pycharm替换的快捷键_想要快速编写代码,你得熟悉这些快捷键!PyCharm常用快捷键导语工欲善其事必先利其器,想要快速编写代码,就必须要先熟悉快捷键,Python开发利器Pycharm常用快捷键如下,相信有了这些快捷键,你编写代码会事半功倍。1编辑Shift+F1外部文档Shift+Enter另起一行Alt+Enter快速修正Alt+Insert自动生成代码Ctrl+O重新方法Ctrl+Alt+T选中Ct…

    2022年8月26日
    4
  • python缩进格式错误的是_python 缩进错误,

    展开全部要求严2113格的代码缩进是python语法的一大特色,就像C语言5261家族(C、C++、Java、C#等等)中的花括4102号一1653样重要,在大多数场合还有必要。在很多代码规范里面也都有要求代码书写按照一定的规则进行换行和代码缩进,但是这些要求只是纯粹是方便人(程序员)来阅读、使用或修改的,对于编译器或者解释器而言,完全是视而不见的。但是对Python解释器而言,每行代码前的缩进都…

    2022年4月12日
    31
  • SSDP协议_Smb协议

    SSDP协议_Smb协议SSDP就是简单服务发现协议(SimpleServiceDiscoveryProtocol)是一种应用层协议,它是构成通用即插即用(也就是UPnP,UPnP是各种各样的智能设备、无线设备和个人电脑等实现遍布全球的对等网络连接的结构)技术的核心协议之一。    简单服务发现协议提供了在局部网络里面发现设备的机制。控制点(也就是接受服务的客户端)能够直接通过使用简单服务发现协议,根据自己的需要查询…

    2022年10月11日
    0
  • createfont函数_windows程序设计基于.net平台

    createfont函数_windows程序设计基于.net平台CFont * f; f = new CFont; f->CreateFont(10, // nHeight 0, // nWidth 0, // nEscapement 0, // nOrientation FW_BOLD, // nWeight FALSE, // bItalic …

    2022年8月18日
    3

发表回复

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

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