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);

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 论坛提问艺术

    论坛提问艺术1明确自己所提的问题是什么,尽可能将问题描述清楚,可以适当加点图形之类的工具辅助一下。2明确问题的类型,然后到正确的论坛提问3标题能够概括所提问题的主要意思4提问要文明礼貌5回答之后要表示感谢…

  • 对象转map(object转map)

    对象转map(object转map)importjava.lang.reflect.Field;importjava.util.LinkedHashMap;importjava.util.Map;publicclassObjectToMap{//对象转MappublicstaticMap<String,Object>getObjectToMap(Objectobj)t…

  • maven配置阿里云镜像仓库「建议收藏」

    maven配置阿里云镜像仓库「建议收藏」1.修改maven根目录下的conf文件夹中的setting.xml文件<mirrors><mirror><id>alimaven</id><name>aliyunmaven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><mirrorOf&g.

  • verycd下载办法_不提供是什么意思

    verycd下载办法_不提供是什么意思现在verycd不提供下载了,我们少了一个重要的资源下载地址。以前是影视剧不能下载,现在是所有东西全部不可以下载。难道我们没有其他方可以下载吗?还是有的,而且比verycd开放的功能还多。verycd不提供下载的解决办法:simplecd还可以提供下载,甚至影视剧也可以下载。SimpleCD专注于为VeryCD做简单的备份,同时也提供了基本的搜索和列

  • 向下取整和向上取整的符号_python向上取整函数

    向下取整和向上取整的符号_python向上取整函数向上取整,运算称为Ceiling,用数学符号⌈⌉(上有起止,开口向下)表示,。向下取整,运算称为Floor,用数学符号⌊⌋(下有起止,开口向上)表示。注意,向上取整和向下取整是针对有浮点数而言的;若整数向上取整和向下取整,都是整数本身。四舍五入:更接近自己的整数;把小数点后面的数字四舍五入 即:如被舍去部分的头一位数字小于五,则舍去;如大于等于五,则被保留…

    2022年10月22日
  • python中class的定义及使用_python中class的定义及使用

    python中class的定义及使用_python中class的定义及使用因为一直不太清楚面向对象的类和方法的编程思想,所以特地补了一下python-class的知识,在这里记录和分享一下。文章目录类和方法的概念和实例1.python类:`class`2.类的构造方法`__init__()`3.类中方法的参数`self`4.继承5.方法重写类的特殊属性与方法类的私有属性类的私有方法类和方法的概念和实例类(Class):用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。方法:类中定义的函数。类的构造方法__init.

发表回复

您的电子邮箱地址不会被公开。

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