TinyXML-2 是一个轻量级的开源 C++ XML 解析库,用于处理XML数据。
#include"pch.h"
#include <iostream>
#include <string>
#include "tinyxml2.h"
//tinyxml2的类在tinyxml2命名空间
using namespace tinyxml2;
int main()
{
//0、创建对象
tinyxml2::XMLDocument doc;
//1、添加声明---XMLDeclaration*
XMLDeclaration* declaration = doc.NewDeclaration();
//2、创建根节点---XMLElement*
XMLElement* root = doc.NewElement("Root"); doc.InsertEndChild(root);
//3、创建子节点---XMLElement*
<User Name = "fengbingchun">
<Blog>CSDN< / Blog>
<Code>GitHub< / Code>
< / User>
XMLElement* user = doc.NewElement("User");
user->SetAttribute("Name", "feng");//user->Attribute("Name", "feng");
root->InsertEndChild(user);
XMLElement* blog = doc.NewElement("Blog");
XMLText* text1 = doc.NewText("CSDN");
blog->InsertEndChild(text1);
user->InsertEndChild(blog);
XMLElement* code = doc.NewElement("Code");
XMLText* text2 = doc.NewText("GtiHub");
code->InsertEndChild(text2);
user->InsertEndChild(code);
//<Blog Name = "CSDN">
// <Address>https://blog.csdn.net/fengbingchun</Address>
// <ID>fengbingchun< / ID>
//< / Blog>
XMLElement* blog2 = doc.NewElement("Blog");
blog2->SetAttribute("Name", "CSDN");//blog2->Attribute("Name", "CSDN");
root->InsertEndChild(blog2); //设置结束标签: 大节点-》小节点 (父节点-》子节点)
XMLElement* addr = doc.NewElement("Address");
XMLText* text3 = doc.NewText("https://blog.csdn.net/fengbingchun");
addr->InsertEndChild(text3);
blog2->InsertEndChild(addr); // 大节点-》小节点 (父节点-》子节点)
XMLElement* ID = doc.NewElement("ID");
XMLText* text4 = doc.NewText("feng");
ID->InsertEndChild(text4);
blog2->InsertEndChild(ID);
//<Code Name = "GitHub">
// <Address>https://github.com//fengbingchun</Address>
// < Repositories>27 < / Repositories >
//< / Code>
XMLElement* code2 = doc.NewElement("Code");
code2->SetAttribute("Name", "GitHub");
root->InsertEndChild(code2);
XMLElement* add2 = doc.NewElement("Address");
XMLText* text5 = doc.NewText("https://github.com//fengbingchun");
add2->InsertEndChild(text5);
code2->InsertEndChild(add2);
XMLElement* repositories = doc.NewElement("Repositories");
repositories->InsertEndChild(doc.NewText("27"));
code2->InsertEndChild(repositories);
tinyxml2::XMLComment* comment = doc.NewComment("this is a xml test file");
doc.InsertEndChild(comment);
//4、子节点添加属性、内容
//5、保存XML文件
XMLError result = doc.SaveFile("713.xml");//doc.SaveFile("1.xml");
if (result == XML_SUCCESS) printf("XML文件保存成功!\n");
else printf("XML文件保存失败!\n");
}
<Root>
<User Name="feng">
<Blog>CSDN</Blog>
<Code>GtiHub</Code>
</User>
<Blog Name="CSDN">
<Address>https://blog.csdn.net/fengbingchun</Address>
<ID>feng</ID>
</Blog>
<Code Name="GitHub">
<Address>https://github.com//fengbingchun</Address>
<Repositories>27</Repositories>
</Code>
</Root>
<!--this is a xml test file-->
解析XML
<?xml version="1.0" encoding="utf-8"?>
<PCBADiag>
<SYSTEMINFO0 SYSID="870E103C" LOG="TRUE"LOGPATH="D:\BIGDATA\LOG\">
<FWCPU>
<TESTITEM ITEM="CPU0" LOCATION="CPU" CAPTION="CPU TEST" DEVICE="CPU" VALUE="CPU0" ERRORCODE="EF520" TEST="TRUE">
<CONFIG NAME="CPU0" MODEL="AMD64 Family 25 Model 80 Stepping 0" CPUID="178BFBFF00A50F00" CORES="6" L1Cache="384KB" L2Cache="3072KB" L3Cache="16384KB" MANUFACTURER="AuthenticAMD" MAINFREQUENCY="3800MHZ"/>
</TESTITEM>
</FWCPU>
</SYSTEMINFO0>
</PCBADiag>
#include <iostream>
#include <string>
#include "tinyxml2.h"
int getXML(System_Info* system_info, Config* config) {
char SystemInfo[128]; //TCHAR SystemInfo[128];
char SystemID[128]; //用于存主板SSID.
char keyValue[128];
int i = 0; //用于遍历XML项目数量,并存放测试总数量
tinyxml2::XMLDocument doc;
//未获取到主板SSID
if (!GetSSID(SystemID)) {
red("Get SYSID failed!\n");
return 1;
}
//本地XML文件读取
tinyxml2::XMLError ret = doc.LoadFile("DiagConfig.XML");
if (ret == XML_SUCCESS) {
//根节点 <PCBADiag></PCBADiag> *************************** XML必须配置<PCBADiag> *****************************
XMLElement* elmtRoot = doc.RootElement();
for (int sysinfo_number = 0; sysinfo_number < 1024; sysinfo_number++) {
sprintf_s(SystemInfo, "SYSTEMINFO%d", sysinfo_number);
//wsprintf(SystemInfo, "SYSTEMINFO%d", sysinfo_number);
//指定子节点
XMLElement* elmtSystemInfo = elmtRoot->FirstChildElement(SystemInfo);
if (elmtSystemInfo) {
//printf("进入子节点elmtSystemInfo!!\n");
//获取sysid属性
sprintf_s(keyValue, "%s", elmtSystemInfo->Attribute("SYSID"));
//比对SYSID
if (!_stricmp(keyValue, SystemID)) {
XMLElement* elmtCPU = elmtSystemInfo->FirstChildElement("FWCPU");
if (elmtCPU) {
sprintf_s(system_info->SysID, "%s", elmtSystemInfo->Attribute("SysID"));//保存SYSID的值
sprintf_s(system_info->LOG, "%s", elmtSystemInfo->Attribute("LOG"));//保存Log生成判断的值
sprintf_s(system_info->PATH, "%s\\FWCPU.log", elmtSystemInfo->Attribute("LOGPATH"));//保存LOG PATH的值
for (XMLElement* elmtUNIT = elmtCPU->FirstChildElement(); elmtUNIT; elmtUNIT = elmtUNIT->NextSiblingElement()) {
if (elmtUNIT) {
//printf("进入子节点elmtUNIT\n");
//保存ITEM
wsprintf(keyValue, "%s", elmtUNIT->Attribute("ITEM"));
if (keyValue[0] != NULL)
sprintf_s(config[i].ITEM, "%s", keyValue);
//printf("\n\nXML_ITEM%s\n\n", keyValue);
//保存TEST----true
wsprintf(keyValue, "%s", elmtUNIT->Attribute("TEST"));
if (keyValue[0] != NULL)
sprintf_s(config[i].TEST, "%s", keyValue);
//保存ERRORCODE
wsprintf(keyValue, "%s", elmtUNIT->Attribute("ERRORCODE"));
if (keyValue[0] != NULL)
sprintf_s(config[i].ERRORCODE, "%s", keyValue);
//保存VALUE
wsprintf(keyValue, "%s", elmtUNIT->Attribute("VALUE"));
if (keyValue[0] != NULL)
sprintf_s(config[i].VALUE, "%s", keyValue);
//printf("XML_VALUE %s\n", keyValue);
XMLElement* elmtConfig = elmtUNIT->FirstChildElement("CONFIG");
if (elmtConfig) {
//printf("进入子节点elmtConfig!666!\n");
//-------------------- 获取XML需要配置的信息,作比对 --------------------
wsprintf(keyValue, "%s", elmtConfig->Attribute("NAME"));
if (keyValue[0] != NULL)
{
sprintf_s(config[i].NAME, "%s", keyValue);
}
wsprintf(keyValue, "%s", elmtConfig->Attribute("MODEL"));
if (keyValue[0] != NULL)
sprintf_s(config[i].MODEL, "%s", keyValue);
wsprintf(keyValue, "%s", elmtConfig->Attribute("CPUID"));
if (keyValue[0] != NULL)
sprintf_s(config[i].CPUID, "%s", keyValue);
wsprintf(keyValue, "%s", elmtConfig->Attribute("CORES"));
if (keyValue[0] != NULL)
sprintf_s(config[i].CORES, "%s", keyValue);
wsprintf(keyValue, "%s", elmtConfig->Attribute("L1Cache"));
if (keyValue[0] != NULL)
sprintf_s(config[i].L1CACHE, "%s", keyValue);
wsprintf(keyValue, "%s", elmtConfig->Attribute("L2Cache"));
if (keyValue[0] != NULL)
sprintf_s(config[i].L2CACHE, "%s", keyValue);
wsprintf(keyValue, "%s", elmtConfig->Attribute("L3Cache"));
if (keyValue[0] != NULL)
sprintf_s(config[i].L3CACHE, "%s", keyValue);
wsprintf(keyValue, "%s", elmtConfig->Attribute("MANUFACTURER"));
if (keyValue[0] != NULL)
sprintf_s(config[i].MANUFACTURER, "%s", keyValue);
wsprintf(keyValue, "%s", elmtConfig->Attribute("MAINFREQUENCY"));
if (keyValue[0] != NULL)
sprintf_s(config[i].MAINFREQUENCY, "%s", keyValue);
//获取XML文件中CPU测试数量
i++;
}
}
}
}
else {
red("Get CPU config failed!\n");
return 1;
}
//保存XML文件中SATA数量
system_info->Amount = i;
return 0;
}
}
else {
cout << "SYSID:" << SystemID << endl;//DEBUG SYSID
printf("Platform not supported!\n");
return 1;
}
}
}
else if (ret == XML_ERROR_FILE_NOT_FOUND) {
printf("XML file not exist!\n");
return 1;
}
else {
printf("XML file formal error!\n");
return 1;
}
}
【总结:】
1、导入 TinyXML-2 头文件:
#include <tinyxml2.h>
using namespace tinyxml2;
2、创建一个 XMLDocument 对象,用于加载和解析 XML 文件:
tinyxml2::XMLDocument xmlDoc;
3、使用 LoadFile 方法加载 XML 文件:
tinyxml2::XMLError result = xmlDoc.LoadFile("1.xml");
4、检查加载结果以确保 XML 文件成功加载:
if (result == XML_SUCCESS) {
// XML 文件加载成功
}
else if (ret = = XML_ERROR_FILE_NOT_FOUND) {
printf("XML 文件加载失败!\n");
return 1;
}
else {
printf("XML 内容格式错误!\n");
return 1;
}
5、获取根节点:
tinyxml2::XMLNode* rootNode = xmlDoc.FirstChildElement("Root");
6、遍历子节点或获取特定子节点的属性或文本内容:文章来源:https://www.toymoban.com/news/detail-568975.html
// 遍历所有子节点
for (tinyxml2::XMLElement* child = rootNode->FirstChildElement(); child != nullptr; child = child->NextSiblingElement()) {
// 子节点处理逻辑
}
// 获取特定子节点的属性
const char* attributeValue = rootNode->FirstChildElement("Child")->Attribute("attributeName");
// 获取特定子节点的文本内容
const char* textValue = rootNode->FirstChildElement("Child")->GetText();
生成XML
#include <iostream>
#include <string>
#include "tinyxml2.h"
//tinyxml2的类在tinyxml2命名空间
using namespace tinyxml2;
int main()
{
//1、创建XML文档对象
tinyxml2::XMLDocument doc; //XMLDocument doc;
//2、创建根节点
XMLNode* rootNode = doc.NewElement("Root");
doc.InsertEndChild(rootNode); // <Root>...</Root>
//3、创建子节点并添加到根节点下
XMLElement* childElement = doc.NewElement("Child");
childElement->SetText("Hello World");
rootNode->InsertEndChild(childElement);
//4、保存XML文件
XMLError result = doc.SaveFile("1.xml");
if (result==XML_SUCCESS)
{
printf("XML文件保存成功!\n");
}
else
{
printf("XML文件保存失败。\n");
}
return 0;
}
展示效果:文章来源地址https://www.toymoban.com/news/detail-568975.html
<?xml version="1.0"?>
<Root>
<Child>Hello World</Child>
</Root>
到了这里,关于C++ XML 库 TinyXML2 的基本使用(案例版)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!