C++实现对XML文件的读写操作

本文介绍如何使用TinyXML库在C++环境中创建和读取XML文件。通过实例代码演示了XML文件的基本构建方法及如何解析已有的XML文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、预先准备

  • 操作系统:win10(unix也可按照此操作)
  • 编译器:gcc/g++
  • 软件编辑器:Visual Studio Code
  • 依赖文件:tinyXml

注意:读者需事先安装配置gcc/g++,具体细节可参考https://jingyan.baidu.com/article/ab0b563085d2a8c15afa7da4.html

二、使用方法

新建项目文件夹(例如:test),将下载好的tinyXml解压,解压后将下图中的6个文件复制到项目文件夹中。
图一
使用vscode打开项目文件夹,新建test.cpp文件,并将下面的代码粘贴至test.cpp文件中。项目文件下载链接:https://github.com/hlc0216/xml_wr
在这里插入图片描述

#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
#include "tinyxml.h"
#include <iostream>
#include <cstring>
using namespace std;
 
/*
	TiXmlDocument:文档类,它代表了整个xml文件
	TiXmlDeclaration:声明类,它表示文件的声明部分
	TiXmlComment:注释类,它表示文件的注释部分
	TiXmlElement:元素类,它是文件的主要部分,并且支持嵌套结构,一般使用这种结构来分类的存储信息,它可以包含属性类和文本类
	TiXmlAttribute/TiXmlAttributeSet:元素属性,它一般嵌套在元素中,用于记录此元素的一些属性
	TiXmlText:文本对象,它嵌套在某个元素内部
*/
//创建xml文件
int writeXmlFile()
{
	TiXmlDocument *writeDoc = new TiXmlDocument; //xml文档指针
	
	//文档格式声明
	TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "UTF-8", "yes");
	writeDoc->LinkEndChild(decl); //写入文档
 
	int n = 3;	//父节点个数
 
	TiXmlElement *RootElement = new TiXmlElement("Info");//根元素
	RootElement->SetAttribute("num", n); //属性
	writeDoc->LinkEndChild(RootElement);
	
	for(int i=0; i<n; i++)//n个父节点
	{
		TiXmlElement *StuElement = new TiXmlElement("Stu");//Stu
		//设置属性
		StuElement->SetAttribute("class","A");
		if(2 == i)
		{
				StuElement->SetAttribute("class","B");
		}
		
		StuElement->SetAttribute("id",i+1);
		StuElement->SetAttribute("flag", (i+1)*10);
		RootElement->LinkEndChild(StuElement);//父节点写入文档
	
		//姓名
		TiXmlElement *nameElement = new TiXmlElement("name");
		StuElement->LinkEndChild(nameElement);
 
		TiXmlText *nameContent = new TiXmlText("mike");
		nameElement->LinkEndChild(nameContent);
		
		//分数
		TiXmlElement *scoreElement = new TiXmlElement("score");
		StuElement->LinkEndChild(scoreElement);
 
		TiXmlText *scoreContent = new TiXmlText("88");
		scoreElement->LinkEndChild(scoreContent);
		
		//城市
		TiXmlElement *cityElement = new TiXmlElement("city");
		StuElement->LinkEndChild(cityElement);
 
		TiXmlText *cityContent = new TiXmlText("Shenzhen");
		cityElement->LinkEndChild(cityContent);
		
	}
	
	writeDoc->SaveFile("stu_info.xml");
	delete writeDoc;
	
	return 1;
}
 
//解析xml文件
int readXmlFile()
{
	TiXmlDocument mydoc("stu_info.xml");//xml文档对象
	bool loadOk=mydoc.LoadFile();//加载文档
	if(!loadOk)
	{
		cout<<"could not load the test file.Error:"<<mydoc.ErrorDesc()<<endl;
		exit(1);
	}
 
	TiXmlElement *RootElement=mydoc.RootElement();	//根元素, Info
	cout<< "[root name]" << RootElement->Value() <<"\n";
	
	TiXmlElement *pEle=RootElement;
 
	//遍历该结点
	for(TiXmlElement *StuElement = pEle->FirstChildElement();//第一个子元素
		StuElement != NULL;
		StuElement = StuElement->NextSiblingElement())//下一个兄弟元素
	{
		// StuElement->Value() 节点名称
		cout<< StuElement->Value() <<" ";
		TiXmlAttribute *pAttr=StuElement->FirstAttribute();//第一个属性
		
		while( NULL != pAttr) //输出所有属性
		{
			cout<<pAttr->Name()<<":"<<pAttr->Value()<<" ";
			pAttr=pAttr->Next();
		}
		cout<<endl;
		
		//输出子元素的值
		for(TiXmlElement *sonElement=StuElement->FirstChildElement();
		sonElement;
		sonElement=sonElement->NextSiblingElement())
		{
			cout<<sonElement->FirstChild()->Value()<<endl;
		}
	}
	
	return 1;
}
 
int main(int argc, char *argv[])
{
	
	writeXmlFile();
	printf("\nafter write\n");
	readXmlFile();
	return 0;
}

三、编译运行

单击test.cpp,右键选择在终端中打开,输入g++ *.cpp,或者gcc *.cpp后会生成a.exe,再输入./a,终端就会有输出结果。
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲸落寰宇

小编会不断努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值