Windows10 VS2017 C++ xml解析(tinyxml2库)

简介: 版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/85264849 首先下载tinyxml2 7.0.1库:https://github.com/leethomason/tinyxml2/releases打开tinyxml2,然后升级sdk,解决方案->重定解决方案目标,升级。
版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/85264849

首先下载tinyxml2 7.0.1库:
https://github.com/leethomason/tinyxml2/releases
打开tinyxml2,然后升级sdk,解决方案->重定解决方案目标,升级。
然后编译生成dll和库文件,在tinyxml2\Debug-Dll下,将tinyxml2.lib和tinyxml2.dll拷贝到新建的工程目录,在新建工程根目录新建include文件夹,将tinyxml2.h拷入,并将工程->配置属性->vc++目录->包含目录指向此目录。
编码:


#include "pch.h"
#include <iostream>
#include <tinyxml2.h>

#pragma comment(lib, "tinyxml2.lib")

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

如果需要单独使用exe,需要注意带上tinyxml2.dll文件
结果显示:
在这里插入图片描述

参考文章:
https://gist.github.com/felton/5530029

目录
相关文章
|
2月前
|
算法 Linux 开发者
CMake深入解析:打造高效动态链接库路径设置
CMake深入解析:打造高效动态链接库路径设置
49 0
|
2月前
|
算法 数据处理 开发者
FFmpeg库的使用与深度解析:解码音频流流程
FFmpeg库的使用与深度解析:解码音频流流程
37 0
|
2月前
|
XML 前端开发 数据格式
请描述如何使用`BeautifulSoup`或其他类似的库来解析 HTML 或 XML 数据。
【2月更文挑战第22天】【2月更文挑战第67篇】请描述如何使用`BeautifulSoup`或其他类似的库来解析 HTML 或 XML 数据。
|
2月前
|
Java 调度 Python
深入解析 Python asyncio 库:如何使用线程池实现高效异步编程
深入解析 Python asyncio 库:如何使用线程池实现高效异步编程
64 0
|
24天前
|
人工智能 机器人 C++
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
【C++/Python】Windows用Swig实现C++调用Python(史上最简单详细,80岁看了都会操作)
|
3月前
|
存储 数据可视化 数据挖掘
Python在数据分析中的利器:Pandas库全面解析
【2月更文挑战第7天】 众所周知,Python作为一种简洁、易学且功能强大的编程语言,被广泛运用于数据科学和人工智能领域。而Pandas库作为Python中最受欢迎的数据处理库之一,在数据分析中扮演着举足轻重的角色。本文将全面解析Pandas库的基本功能、高级应用以及实际案例,带您深入了解这个在数据分析领域的利器。
57 1
|
24天前
|
人工智能 机器人 编译器
【C++】Windows端VS code中运行CMake工程(手把手教学)
【C++】Windows端VS code中运行CMake工程(手把手教学)
|
29天前
|
关系型数据库 数据库 C++
【C++】Windows使用Visual Studio C++链接云数据库PostgreSQL(沉浸式老爷教学)
【C++】Windows使用Visual Studio C++链接云数据库PostgreSQL(沉浸式老爷教学)
|
2月前
|
缓存 算法 C语言
【C++ 标准查找算法 】C++标准库查找算法深入解析(In-depth Analysis of C++ Standard Library Search Algorithms)
【C++ 标准查找算法 】C++标准库查找算法深入解析(In-depth Analysis of C++ Standard Library Search Algorithms)
48 0
|
2天前
|
网络协议 Windows
Windows Server 各版本搭建 DNS 服务器实现域名正反向解析
Windows Server 各版本搭建 DNS 服务器实现域名正反向解析

推荐镜像

更多