TinyXML2

XML指可扩展标记语言(eXtensible Markup Language), 被设计用来传输和存储数据。

C++语言环境下有许多用于 XML 解析的第三方库,例如 libxml++, TinyXML2 等.

如何选择合适的 XML 解析库: What XML parser should I use in C++? [closed] - stackoverflow.

其中 TinyXML2 是一个轻量级的 xml 解析库, 只有一个源文件 tinyxml2.cpp 和一个头文件 tinyxml2.h, 十分简单易用.
最简单的调库方法就是把头文件和和源文件添加到自己的工程中一起编译.
当然也可以将 TinyXML2 单独编译成链接库供其他工程使用.

下载

从 Github 的 Release 中下载发行版tinyxml2-7.0.1.zip.

解压后得到如下文件.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
tinyxml2
│ appveyor.yml
│ readme.md
│ tinyxml2.cpp
│ tinyxml2.h
│ xmltest.cpp
│ ...
├─contrib
│ html5-printer.cpp
├─docs // 文档
│ │ index.html
│ │ bc_s.png
│ │ bdwn.png
│ │ classes.html
│ │ classtinyxml2_1_1_x_m_l_attribute-members.html
│ │ ...
│ └─search
│ all_0.html
│ all_0.js
│ all_1.html
│ all_1.js
│ all_10.html
│ all_10.js
│ ...
├─resources
│ │ dream.xml
│ │ empty.xml
│ │ ...
│ └─out
│ readme.txt
└─tinyxml2
│ test.vcxproj
│ test.vcxproj.filters
│ tinyxml2.sln // 解决方案文件
│ tinyxml2.vcxproj
│ tinyxml2.vcxproj.filters
├─tinyxml2-cbp
│ README
│ tinyxml2-cbp.cbp
└─tinyxml2.xcodeproj
project.pbxproj

配置

双击其中的 tinyxml2/tinyxml2/tinyxml2.sln 打开解决方案.

初始配置的 SDK 版本也许与本地版本不一致, 因此点击 项目 -> 重定解决方案目标 , 选择已安装的 SDK 版本, 勾选所需项目, 点击 确定.

生成库文件

Debug版本为例.

  1. 解决方案配置 下拉框中选择 Debug-Dll, 解决方案平台 选择为x64, 生成解决方案.
  2. 然后再将 解决方案配置 下拉框改为 Debug-Lib, 再生成一次. 两次操作会在解决方案文件相同路径下生成bin\ 文件夹, 如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    bin
    ├─test
    │ ├─x64-Debug-Dll
    │ │ test.exe
    │ │ test.ilk
    │ │ test.pdb
    │ │
    │ └─x64-Debug-Lib
    │ test.exe
    │ test.ilk
    │ test.pdb

    └─tinyxml2
    ├─x64-Debug-Dll
    │ tinyxml2.dll
    │ tinyxml2.exp
    │ tinyxml2.ilk
    │ tinyxml2.lib
    │ tinyxml2.pdb

    └─x64-Debug-Lib
    tinyxml2.lib

测试官方代码

resources\文件夹复制到解决方案文件所在目录, 将 test 项目 设为启动项目, 开始执行.

测试库文件

另外新建解决方案, 新建工程, 将前面生成的库文件

1
2
3
4
5
6
7
bin
└─tinyxml2
└─x64-Debug-Dll
tinyxml2.dll
tinyxml2.lib
tinyxml2.pdb

和头文件

1
- tinyxml2.h

添加到工程中, 就可以调用 TinyXML-2 库了.

一个简单的用于测试的源码文件如下.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//tinyxml2 测试代码
#include <iostream>
#include "tinyxml2.h"
using namespace std;
int main()
{
static const char* xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
"<entries>"
"<entry name=\"My First Post\" age=\"52\">I believe every human has a finite number of heartbeats. I don't intend to waste any of mine</entry>"
"<entry name=\"The Second\" age=\"\">You know, being a test pilot isn't always the healthiest business in the world.</entry>"
"<entry>Entry</entry>"
"<entry name=\"The Third\" secretdata=\"9809832\">We have an infinite amount to learn both from nature and from each other</entry>"
"<entry name=\"Final Post...\" hidden=\"true\" age=\"3\">Across the sea of space, the stars are other suns.</entry>"
"</entries>";

tinyxml2::XMLDocument doc;
doc.Parse(xml);

tinyxml2::XMLHandle docHandle(&doc);

tinyxml2::XMLElement *entry = docHandle.FirstChildElement("entries").ToElement();

if (entry) {
for (tinyxml2::XMLNode *node = entry->FirstChildElement(); node; node = node->NextSibling()) {
tinyxml2::XMLElement *e = node->ToElement();

const char *name = e->Attribute("name");
if (name) cout << name << ": ";

cout << e->GetText();

int true_age = e->IntAttribute("age") + 50;

cout << " " << true_age << endl;
}
}
return 0;
}

教程文档

tinyxml2\docs\index.html中有关于该库的离线文档, 可以直接阅读.

文件读写

1
2
3
4
5
6
7
8
tinyxml2::XMLDocument doc;
if (doc.LoadFile("input.xml") != 0)
{
cout << "load xml file failed" << endl;
return;
}
doc.Print();
doc.SaveFile("output.xml");

注意

  1. 官方 Github 仓库中不同版本的 tinyxml2 是使用不同版本的 Visual Studio 平台工具集构建的,若官方的版本与自己本地的版本不匹配可能报错,参考:error MSB8020: 无法找到 v142 的生成工具 - CSDN. 对于 VS2017+V141 toolset 使用 tinyxml2-7.0.1 就可以了, 不建议使用更高版本.
  2. tinyxml2相对于 tinyxml 重写了全部代码, 除非是为了维护老旧代码, 否则请直接使用tinyxml2.
  3. TinyXML-2既不依赖也不支持 STL, 通过返回const char* 来实现更高的内存利用效率.

参考

作者

Luo Siyou

发布于

2023-01-09

更新于

2023-01-26

许可协议