
C++实现对XML文件的读写操作
发布日期:2021-05-10 23:21:19
浏览次数:28
分类:精选文章
本文共 3096 字,大约阅读时间需要 10 分钟。
预先准备
操作系统:建议使用 Win10(UNIX 环境也可按此操作)
编译器:使用 gcc/g++
软件编辑器:Visual Studio Code依赖文件:完整的依赖文件请参考官方文档安装配置
注意:读者需事先安装配置 gcc/g++,具体细节可参考安装指南。
使用方法
新建项目文件夹(例如:test),将 tinyXml 解压后,将以下6个文件复制到项目文件夹中。
文件列表:- tinyxml.h
- TiXmlDocument.h
- TiXmlDeclaration.h
- TiXml Comment.h
- TiXmlElement.h
- TiXmlAttribute.h
使用 Visual Studio Code 打开项目文件夹,新建 test.cpp 文件,将以下代码粘贴至 test.cpp 文件中。
代码示例:
#include#include "tinyxml.h" #include #include using namespace std;// 创建 XML 文件 int writeXmlFile() { TiXmlDocument *writeDoc = new TiXmlDocument; // 声明部分 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) { create stu element TiXmlElement *stuElement = new TiXmlElement("stu"); stuElement->SetAttribute("class", "A"); if (i == 2) { 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"); bool loadOk = myDoc.LoadFile(); if (!loadOk) { cout << "Could not load the test file. Error:" << myDoc.ErrorMessage() << endl; exit(1); } // 获取根元素 TiXmlElement *rootElement = myDoc.RootElement(); cout << "[root name]" << rootElement->Value() << endl; // 遍历子节点 TiXmlElement *pEle = rootElement; TiXmlElement *stuElement = pEle->FirstChildElement(); while (stuElement != NULL) { cout << stuElement->Value() << " "; // 输出属性 TiXmlAttribute *pAttr = stuElement->FirstAttribute(); while (pAttr != NULL) { cout << pAttr->Name() << ":" << pAttr->Value() << " "; pAttr = pAttr->Next(); } // 获取子元素 stuElement = stuElement->NextSiblingElement(); pEle = stuElement->FirstChildElement(); } return 1; }
编译运行
单击 test.cpp 文件右键选择“在终端中打开”,输入以下命令:
g++ *.cpp
或(如果使用 gcc):
gcc *.cpp
生成可执行文件 a.exe 后,输入以下命令运行:
./a
完成!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月28日 04时37分56秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
angular2项目里使用排他思想
2021-05-18
折线图上放面积并隐藏XY轴的线
2021-05-18
zabbix之自动发现
2019-03-15
Experience of tecent interview
2019-03-15
python实验--太理二
2019-03-15
failed to push some refs to git
2019-03-15
vue基础学习01
2019-03-15
控制流程获取1-100以内的质数质数
2019-03-15
在苹果Mac上如何更改AirDrop名称?
2019-03-15
1110 Complete Binary Tree (25 point(s))
2019-03-15
310【毕设课设】微机原理-基于8086CPU校园标语牌系统仿真设计
2019-03-15
541【毕设课设】基于单片机电阻电感电容RLC测量仪系统
2019-03-15
568【毕设课设】基于单片机多路温度采集显示报警控制系统设计
2019-03-15
基于8086交通灯系统仿真设计(微机原理设计资料)
2019-03-15
TCP/IP五层模型之数据链路层
2019-03-15
解读域名管理之:域名注册机构介绍
2019-03-15
找中位数
2019-03-15
这些运维发展方向及系统运维技能都不了解,怎么能吃透Linux??
2019-03-15
干货.一文快速告诉你API功能测试怎样做才能快捷高效!
2019-03-15
自动化测试——UI自动化测试的痛点
2019-03-15