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

本文涉及的产品
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
简介: 版权声明:本文可能为博主原创文章,若标明出处可随便转载。 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;
}
AI 代码解读

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

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

目录
打赏
0
0
0
0
11
分享
相关文章
|
6月前
|
C++标准库(速查)总结
C++标准库(速查)总结
145 6
|
6月前
|
C++ STL 初探:打开标准模板库的大门
C++ STL 初探:打开标准模板库的大门
160 10
超级好用的C++实用库之服务包装类
通过本文对Boost.Asio、gRPC和Poco三个超级好用的C++服务包装类库的详细介绍,开发者可以根据自己的需求选择合适的库来简化开发工作,提高代码的效率和可维护性。每个库都有其独特的优势和适用场景,合理使用这些库可以极大地提升C++开发的生产力。
68 11
AI Dev Gallery:微软开源 Windows AI 模型本地运行工具包和示例库,助理开发者快速集成 AI 功能
微软推出的AI Dev Gallery,为Windows开发者提供开源AI工具包和示例库,支持本地运行AI模型,提升开发效率。
157 13
|
6月前
|
C++常用基础知识—STL库(2)
C++常用基础知识—STL库(2)
107 5
C++常用基础知识—STL库(1)
C++常用基础知识—STL库(1)
115 1
超级好用的C++实用库之跨平台实用方法
超级好用的C++实用库之跨平台实用方法
73 6
|
7月前
|
超级好用的C++实用库之环形内存池
超级好用的C++实用库之环形内存池
137 5
超级好用的C++实用库之套接字
超级好用的C++实用库之套接字
63 1
超级好用的C++实用库之国密sm4算法
超级好用的C++实用库之国密sm4算法
207 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等