TinyXML2是一个开源、简单、小巧、高效的C++ XML解析器,它只有一个.h文件和一个.cpp文件组成,可以轻松集成到其它程序中。它解析XML文档并从中构建可以读取、修改和保存的文档对象模型(Document Object Model, DOM)。它不能解析DTD(Document Type Definitions, 文档类型定义)或XSL(eXtensible Stylesheet Language, 扩展样式表语言)。在TinyXML2中,XML数据被解析为可以浏览和操作的C++对象,然后写入磁盘和其它输出流。它不依赖于C++的STL。

TinyXML2的license为ZLib,可以商用,它的源码在https://github.com/leethomason/tinyxml2 ,最新发布版本为7.1.0。

关于XML的介绍可以参考:https://blog.csdn.net/fengbingchun/article/details/38978591

以下是测试代码(test_tinyxml2.cpp):创建XML(test_tinyxml2_create)和解析XML(test_tinyxml2_parse)

#include "funset.hpp"
#include <iostream>
#include "tinyxml2.h"int test_tinyxml2_create()
{const char* declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";tinyxml2::XMLDocument doc;tinyxml2::XMLError ret = doc.Parse(declaration);if (ret != 0) {fprintf(stderr, "fail to parse xml file: %s\n", declaration);return -1;}tinyxml2::XMLComment* comment = doc.NewComment("this is a xml test file");doc.InsertEndChild(comment);tinyxml2::XMLElement* root = doc.NewElement("Root");doc.InsertEndChild(root);// Usertinyxml2::XMLElement* user = doc.NewElement("User");user->SetAttribute("Name", "fengbingchun");root->InsertEndChild(user);tinyxml2::XMLElement* blog = doc.NewElement("Blog");tinyxml2::XMLText* text1 = doc.NewText("CSDN");blog->InsertEndChild(text1);user->InsertEndChild(blog);tinyxml2::XMLElement* code = doc.NewElement("Code");tinyxml2::XMLText* text2 = doc.NewText("GitHub");code->InsertEndChild(text2);user->InsertEndChild(code);// Blogtinyxml2::XMLElement* blog2 = doc.NewElement("Blog");blog2->SetAttribute("Name", "CSDN");root->InsertEndChild(blog2);tinyxml2::XMLElement* addr = doc.NewElement("Address");tinyxml2::XMLText* text3 = doc.NewText("https://blog.csdn.net/fengbingchun");addr->InsertEndChild(text3);blog2->InsertEndChild(addr);tinyxml2::XMLElement* id = doc.NewElement("ID");tinyxml2::XMLText* text4 = doc.NewText("fengbingchun");id->InsertEndChild(text4);blog2->InsertEndChild(id);// Codetinyxml2::XMLElement* code2 = doc.NewElement("Code");code2->SetAttribute("Name", "GitHub");root->InsertEndChild(code2);tinyxml2::XMLElement* addr2 = doc.NewElement("Address");tinyxml2::XMLText* text5 = doc.NewText("https://github.com//fengbingchun");addr2->InsertEndChild(text5);code2->InsertEndChild(addr2);tinyxml2::XMLElement* repositories = doc.NewElement("Repositories");tinyxml2::XMLText* text6 = doc.NewText("27");repositories->InsertEndChild(text6);code2->InsertEndChild(repositories);#ifdef _MSC_VERconst char* file_name = "E:/GitCode/Messy_Test/testdata/test.xml";
#elseconst char* file_name = "testdata/test.xml";
#endifret = doc.SaveFile(file_name);if (ret != 0) {fprintf(stderr, "fail to save xml file: %s\n", file_name);return -1;}return 0;
}int test_tinyxml2_parse()
{
#ifdef _MSC_VERconst char* file_name = "E:/GitCode/Messy_Test/testdata/test_tinyxml2.xml";
#elseconst char* file_name = "testdata/test_tinyxml2.xml";
#endiftinyxml2::XMLDocument doc;tinyxml2::XMLError ret = doc.LoadFile(file_name);if (ret != 0) {fprintf(stderr, "fail to load xml file: %s\n", file_name);return -1;}tinyxml2::XMLElement* root = doc.RootElement();fprintf(stdout, "root element name: %s\n", root->Name());// Usertinyxml2::XMLElement* user = root->FirstChildElement("User");if (!user) {fprintf(stderr, "no child element: User\n");return -1;}fprintf(stdout, "user name: %s\n", user->Attribute("Name"));tinyxml2::XMLElement* blog = user->FirstChildElement("Blog");if (!blog) {fprintf(stderr, "no child element: Blog, in User\n");return -1;}fprintf(stdout, "blog value: %s\n", blog->GetText());fprintf(stdout, "code value: %s\n\n", user->FirstChildElement("Code")->GetText());// Blogtinyxml2::XMLElement* blog2 = root->FirstChildElement("Blog");if (!blog2) {fprintf(stderr, "no child element: Blog\n");return -1;}fprintf(stdout, "blog name: %s\n", blog2->Attribute("Name"));tinyxml2::XMLElement* addr = blog2->FirstChildElement("Address");if (!addr) {fprintf(stderr, "no child element: Address, in Blog\n");return -1;}fprintf(stdout, "address value: %s\n", addr->GetText());fprintf(stdout, "id value: %s\n\n", blog2->FirstChildElement("ID")->GetText());// Codetinyxml2::XMLElement* code = root->FirstChildElement("Code");if (!code) {fprintf(stderr, "no child element: Code\n");return -1;}fprintf(stdout, "code name: %s\n", code->Attribute("Name"));tinyxml2::XMLElement* addr2 = code->FirstChildElement("Address");if (!addr2) {fprintf(stderr, "no child element: Address, in Code\n");return -1;}fprintf(stdout, "address value: %s\n", addr2->GetText());fprintf(stdout, "repositories value: %s\n\n", code->FirstChildElement("Repositories")->GetText());return 0;
}

创建xml文件的执行结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--this is a xml test file-->
<Root><User Name="fengbingchun"><Blog>CSDN</Blog><Code>GitHub</Code></User><Blog Name="CSDN"><Address>https://blog.csdn.net/fengbingchun</Address><ID>fengbingchun</ID></Blog><Code Name="GitHub"><Address>https://github.com//fengbingchun</Address><Repositories>27</Repositories></Code>
</Root>

解析以上xml文件输出结果如下:

GitHub:https://github.com/fengbingchun/Messy_Test

开源库TinyXML2简介及使用相关推荐

  1. 开源库jemalloc简介

    jemalloc是通用的malloc(3)实现,它强调避免碎片和可扩展的并发支持.它的源码位于https://github.com/jemalloc/jemalloc,最新稳定版本为5.2.1. gl ...

  2. 开源库libuuid简介及使用

    libuuid是一个开源的用于生成UUID(Universally Unique Identifier,通用唯一标识符)的库,它的源码可从https://sourceforge.net/project ...

  3. xml格式文件解析开源库tinyxml2使用

    tinyxml2需要用到tinyxml2.h及tinyxml2.cpp文件,下载地址:tinyxml2,下载后放在程序根目录下. 头文件添加: #include "tinyxml2.h&qu ...

  4. YAML开源库yaml-cpp简介及使用

    关于YAML的介绍可以参考:https://blog.csdn.net/fengbingchun/article/details/88090609 yaml-cpp是用c++实现的用来解析和生成yam ...

  5. apache madlib 教程_Apache顶级开源项目——机器学习库MADlib简介与应用实例

    原标题:Apache顶级开源项目--机器学习库MADlib简介与应用实例 Apache MADlib是Pivotal与UCBerkeley合作的一个开源机器学习库,提供了精确的数据并行实现.统计和机器 ...

  6. 《Android开源库 ~ 1》 GitHub Android Libraries Top 100 简介

    转载自GitHub Android Libraries Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitH ...

  7. 强大的Canvas开源库Fabric.js简介与开发指南

    什么是Fabric.js? Fabric.js 是一个强大且简单的Javascript HTML5 Canvas库. 官网地址:http://fabricjs.com/ 为什么要使用Fabric.js ...

  8. 纠删码开源软件Jerasure库接口简介及性能测试

    原文见链接基于柯西矩阵的Erasure Code技术详解 原文见链接基于范德蒙矩阵的Erasure code技术详解 原文见链接一个IO的传奇一生(12)-- 磁盘阵列1 原文见链接Microsoft ...

  9. 常用C/C++开源库

    1. 框架 Apache C++ Standard Library : 是一系列算法,容器,迭代器和其他基本组件的集合 ASL : Adobe源代码库提供了同行的评审和可移植的C++源代码库. Boo ...

  10. android思维导图github,2020年GitHub 上那些优秀Android开源库,这里是Top10!

    前言 每过一段时间呀,我都会给大家带来一些从Github上收集的一些开源库,有的是炫酷动效,有的则是实用的工具和类库.以前没看过或者没有收藏的同学,建议先收藏,以下是链接: No1. LiquidSw ...

最新文章

  1. 钉钉被小学生逼疯,拍片在线求饶哈哈哈哈
  2. iOS-SDPhotoBrowser
  3. mysql 监控工具
  4. 柏林噪声产生火焰等纹理
  5. AGG第三十五课 gsv_text 渲染ASCII字符
  6. Eclipse安装插件时报No repository found containing...解决办法
  7. 网页载入动画 php,网站页面加载动画代码
  8. Linux下 对文件行数打乱(乱序排列)
  9. Socket开发框架之框架设计及分析
  10. php 监听模式,总结Laravel事件系统用法(监听事件,观察者模式)
  11. Visio2013 Professional专业版密钥
  12. 记录一次使用Aliyun OSS 存图片
  13. 批判性思维_为什么批判性思维技能对数据科学家至关重要
  14. 计算机毕业设计 安卓 Android studio音乐播放器app 仿酷狗,仿网易云音乐播放器
  15. httpwatch详解
  16. 新猿木子李:0基础学python培训教程 Python操作Excel之写入数据
  17. Uipath 对Excel中重复的行进行筛选解决方案以及DefaultVaule方法的使用
  18. 利用 EXE4j 生成 .exe Java Swing程序
  19. 高斯投影坐标计算例题_测量学高斯投影已知横坐标如何求在第几度带投影计算而得的?例如:...-y坐标的自然值怎么算-数学-莫囤料同学...
  20. does not specify a Swift version and none of the targets (`packager`) integrating it have the `SWIFT

热门文章

  1. 阿里妈妈展示广告引擎新探索:迈向全局最优算力分配
  2. edge浏览器internet选项的设置方法
  3. drop index mysql_MySQL修改和删除索引(DROP INDEX)
  4. 转变为灰度图像的算法优化及马赛克实现代码
  5. 双显卡笔记本安装Ubuntu系统
  6. 为Visual Studio创建项目模板(VSIX / C#/ 2019)
  7. pandas的自带数据集_Pandas教程:初学者入门必备,很全面,很详细!
  8. 如何进行网站挂马检测?怎样清除挂马?
  9. 打印机共享计算机密码,打印机共享需要密码怎么办?
  10. PMP-强化练习题一(180题答案及解析)