Windows10 VS2017 C++ xml解析(tinyxml2庫)
首先下載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檔案
結果顯示:
相關文章
- Windows10 VS2017 C++ Json解析(使用jsoncpp庫)WindowsC++JSON
- Windows10 VS2017 C++ ini解析(使用simpleini標頭檔案)WindowsC++
- Windows10 VS2017 C++使用crypto++庫加密解密(AES)WindowsC++加密解密
- windows10 vs2017 C++連線MySQLWindowsC++MySql
- Windows10 VS2017 C++訊號處理WindowsC++
- Windows10 VS2017 C++編譯Linux程式WindowsC++編譯Linux
- Windows10 VS2017 C++模擬點選按鍵WindowsC++
- Windows10 VS2017 C++去除unsafe提示得最簡單方法WindowsC++
- NPM酷庫051:xml2js,解析XML格式資料NPMXMLJS
- Xml解析XML
- VS2017 MFC C++入門C++
- Windows10 VS2017 C++ Server Socket簡單伺服器端與客戶端WindowsC++Server伺服器客戶端
- Java解析XMLJavaXML
- go 解析xmlGoXML
- iOS – XML解析iOSXML
- Windows10 VS2017 C++多執行緒傳參和等待執行緒結束WindowsC++執行緒
- python之XML解析PythonXML
- 175.XML解析XML
- C# XML解析C#XML
- VS2017無法解析得外部符號符號
- Python XML解析之DOMPythonXML
- python XML 檔案解析PythonXML
- jdom解析xml檔案XML
- XML 檔案解析實踐 (DOM 解析)XML
- C++ | VS2017 C++專案配置使用的常見巨集定義C++
- XML解析-最快的方式SAXXML
- 使用DocumentBuilderFactory解析XML淺談UIXML
- XML DOM 解析器概述XML
- 使用 Java 解析XML檔案JavaXML
- 在VS2017中建立C++的程式碼塊模板C++
- Python解析XML檔案生成HTMLPythonXMLHTML
- 搞懂 XML 解析,徒手造 WEB 框架XMLWeb框架
- 【spring原始碼系列】之【xml解析】Spring原始碼XML
- C/C++ Qt 運用JSON解析庫 [基礎篇]C++QTJSON
- 簡單解析C++基於Boost庫實現命令列C++命令列
- 【Go學習筆記16】解析xmlGo筆記XML
- XML DOM 解析器錯誤概述XML
- java使用jaxb解析XML(含根據xml自動生成實體類)JavaXML