C++中使用TinyXML2读写文件

#include <iostream>
#include"tinyxml2.h"

using namespace std;
//using namespace tinyxml2;

void createXml()
{
	//声明要创建的xml文件
	tinyxml2::XMLDocument xml;
	tinyxml2::XMLDeclaration* declaration = xml.NewDeclaration();
	xml.InsertFirstChild(declaration);

	//创建根节点
	tinyxml2::XMLElement* rootNode = xml.NewElement("computer");
	xml.InsertEndChild(rootNode);

	//创建子节点
	tinyxml2::XMLElement* root_1_NodeComputerMonitor = xml.NewElement("ComputerMonitor");
	tinyxml2::XMLElement* root_1_NodeKeyboard = xml.NewElement("Keyboard");
	tinyxml2::XMLElement* root_1_CPU = xml.NewElement("CPU");

	//给子节点增加内容
	tinyxml2::XMLText* text_root_1_NodeComputerMonitor = xml.NewText("SAMSUNG");
	root_1_NodeComputerMonitor->InsertFirstChild(text_root_1_NodeComputerMonitor);

	tinyxml2::XMLText* text_root_1_root_1_CPU = xml.NewText("intel");
	root_1_CPU->InsertFirstChild(text_root_1_root_1_CPU);

	//给子节点增加属性
	root_1_NodeComputerMonitor->SetAttribute("size", "15");
	root_1_CPU->SetAttribute("series", "XEON");

	//创建子节点的子节点
	tinyxml2::XMLElement* root_2_NodeKeyboardAttribute = xml.NewElement("KeyboardAttribute");

	//给子节点的子节点增加内容
	tinyxml2::XMLText* text_root_2_NodeKeyboardAttribute = xml.NewText("cherry Mechanical Keyboard");
	root_2_NodeKeyboardAttribute->InsertFirstChild(text_root_2_NodeKeyboardAttribute);

	//构建xml树,根节点的下的子节点树
	rootNode->InsertEndChild(root_1_NodeComputerMonitor);//childNodeStu是root的子节点
	rootNode->InsertEndChild(root_1_NodeKeyboard);
	rootNode->InsertEndChild(root_1_CPU);

	//构建xml树,根节点的下的子节点的子节点树
	root_1_NodeKeyboard->InsertEndChild(root_2_NodeKeyboardAttribute);

	//将xml保存到当前项目中
	xml.SaveFile("computer.xml");
}

void deCodeXml()
{
	//声明
	tinyxml2::XMLDocument xml;

	//导入xml文件
	if (xml.LoadFile("computer.xml") != tinyxml2::XML_SUCCESS)
	{
		return;
	}

	//找到导入的xml的根节点
	tinyxml2::XMLElement* rootNode = xml.RootElement();
	if (rootNode == NULL) {
		return;
	}

	//读取第一层子节点信息并且打印在控制台上
	tinyxml2::XMLElement* root_1_NodeComputerMonitor = rootNode->FirstChildElement("ComputerMonitor");
	std::string text_root_1_NodeComputerMonitor = root_1_NodeComputerMonitor->GetText();
	cout << "text_root_1_NodeComputerMonitor = " << text_root_1_NodeComputerMonitor.c_str() << endl;

	//读取第二层子节点信息并且打印在控制台上
	tinyxml2::XMLElement* root_1_NodeKeyboard = rootNode->FirstChildElement("Keyboard");
	tinyxml2::XMLElement* root_2_NodeKeyboardAttribute = root_1_NodeKeyboard->FirstChildElement("KeyboardAttribute");
	std::string text_root_2_NodeKeyboardAttribute = root_2_NodeKeyboardAttribute->GetText();
	cout << "text_root_2_NodeKeyboardAttribute = " << text_root_2_NodeKeyboardAttribute.c_str() << endl;
}
int main()
{
	cout << "----------------------begin create xml-----------------------" << endl;
	createXml();
	cout << "----------------------finished create xml--------------------" << endl;

	cout << "-----------------------begin read xml------------------------" << endl;
	deCodeXml();
	cout << "-----------------------finished read xml---------------------" << endl;
	return 0;
}
  • 输出:
<?xml version="1.0" encoding="UTF-8"?>
<computer>
    <ComputerMonitor size="15">SAMSUNG</ComputerMonitor>
    <Keyboard>
        <KeyboardAttribute>cherry Mechanical Keyboard</KeyboardAttribute>
    </Keyboard>
    <CPU series="XEON">intel</CPU>
</computer>

#include <iostream>
#include <string>
#include"tinyxml2.h"
#include <map>

#define STUDENT_XML_CONFIG   "StudentsInfo.xml"

using namespace std;
//using namespace tinyxml2;

void createXml()
{
	//声明要创建的xml文件
	tinyxml2::XMLDocument xml;
	tinyxml2::XMLDeclaration* declaration = xml.NewDeclaration();
	xml.InsertFirstChild(declaration);

	//创建根节点
	tinyxml2::XMLElement* rootNode = xml.NewElement("Students_Infomation");
	xml.InsertEndChild(rootNode);

	int stuNum = 10;

	//总共有多少条信息
	tinyxml2::XMLElement* stuNumNode = xml.NewElement("Student_Number");
	std::string strNum = std::to_string(stuNum);
	tinyxml2::XMLText* stuNumValue = xml.NewText(strNum.c_str());
	stuNumNode->InsertFirstChild(stuNumValue);
	rootNode->InsertEndChild(stuNumNode);

	//学生总信息
	tinyxml2::XMLElement* stuTotalInfoNode = xml.NewElement("Student_All_Infomation");
	rootNode->InsertEndChild(stuTotalInfoNode);

	for (int i = 0; i < stuNum; ++i)
	{
		//一条完整学生信息
		tinyxml2::XMLElement* infoRow = xml.NewElement("Student");
		char szID[64] = { 0 };
		sprintf(szID, "ID--%d", i);
		infoRow->SetAttribute("ID_Code", szID);

		//子节点信息:姓名
		tinyxml2::XMLElement* stuName = xml.NewElement("Name");
		std::string strName = "ABC--" + std::to_string(i);
		tinyxml2::XMLText* stuNameValue = xml.NewText(strName.c_str());
		stuName->InsertFirstChild(stuNameValue);
		infoRow->InsertEndChild(stuName);

		//子节点信息:年龄
		tinyxml2::XMLElement* stuAge = xml.NewElement("Age");
		std::string strAge = std::to_string(i + 10);
		tinyxml2::XMLText* stuAgeValue = xml.NewText(strAge.c_str());
		stuAge->InsertFirstChild(stuAgeValue);
		infoRow->InsertEndChild(stuAge);

		//性别
		tinyxml2::XMLElement* stuGender = xml.NewElement("Gender");
		std::string strGender = i % 2 ? "female" : "male";
		tinyxml2::XMLText* stuGenderValue = xml.NewText(strGender.c_str());
		stuGender->InsertFirstChild(stuGenderValue);
		infoRow->InsertEndChild(stuGender);

		stuTotalInfoNode->InsertEndChild(infoRow);
	}

	//将xml保存到当前项目中
	xml.SaveFile(STUDENT_XML_CONFIG);
}

void deCodeXml()
{
	//声明
	tinyxml2::XMLDocument xml;

	//导入xml文件
	if (xml.LoadFile(STUDENT_XML_CONFIG) != tinyxml2::XML_SUCCESS)
	{
		return;
	}

	//找到导入的xml的根节点
	tinyxml2::XMLElement* rootNode = xml.RootElement();
	if (rootNode == NULL) {
		return;
	}

	//获取总数
	tinyxml2::XMLElement* stuNumNode = rootNode->FirstChildElement("Student_Number");
	std::string strNum = stuNumNode->GetText();
	int iNum = atoi(strNum.c_str());
	cout << "Student Number = " << iNum << endl;

	//总信息根节点
	tinyxml2::XMLElement* totalInfoNode = rootNode->FirstChildElement("Student_All_Infomation");

	tinyxml2::XMLElement* studentNode = totalInfoNode->FirstChildElement("Student");

	//获取所有信息
	while (studentNode)
	{
		//获取属性
		const tinyxml2::XMLAttribute* attr = studentNode->FindAttribute("ID_Code");

		//获取子节点,输出信息
		tinyxml2::XMLElement* stuNameNode = studentNode->FirstChildElement("Name");
		tinyxml2::XMLElement* stuAgeNode = studentNode->FirstChildElement("Age");
		tinyxml2::XMLElement* stuGenderNode = studentNode->FirstChildElement("Gender");
		if (attr)
		{
			std::cout << "学生编号:" << ":" << attr->Value() \
				<< "  姓名:" << stuNameNode->GetText() \
				<< "  年龄:" << stuAgeNode->GetText() \
				<< "  性别:" << stuGenderNode->GetText()\
				<< std::endl;
		}
		//下一节点
		studentNode = studentNode->NextSiblingElement();
	}
}

int main()
{
	cout << "----------------------begin create xml-----------------------" << endl;
	createXml();
	cout << "----------------------finished create xml--------------------" << endl;

	cout << "-----------------------begin read xml------------------------" << endl;
	deCodeXml();
	cout << "-----------------------finished read xml---------------------" << endl;
	return 0;
}
  • StudentsInfo.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Students_Infomation>
    <Student_Number>10</Student_Number>
    <Student_All_Infomation>
        <Student ID_Code="ID--0">
            <Name>ABC--0</Name>
            <Age>10</Age>
            <Gender>male</Gender>
        </Student>
        <Student ID_Code="ID--1">
            <Name>ABC--1</Name>
            <Age>11</Age>
            <Gender>female</Gender>
        </Student>
        <Student ID_Code="ID--2">
            <Name>ABC--2</Name>
            <Age>12</Age>
            <Gender>male</Gender>
        </Student>
        <Student ID_Code="ID--3">
            <Name>ABC--3</Name>
            <Age>13</Age>
            <Gender>female</Gender>
        </Student>
        <Student ID_Code="ID--4">
            <Name>ABC--4</Name>
            <Age>14</Age>
            <Gender>male</Gender>
        </Student>
        <Student ID_Code="ID--5">
            <Name>ABC--5</Name>
            <Age>15</Age>
            <Gender>female</Gender>
        </Student>
        <Student ID_Code="ID--6">
            <Name>ABC--6</Name>
            <Age>16</Age>
            <Gender>male</Gender>
        </Student>
        <Student ID_Code="ID--7">
            <Name>ABC--7</Name>
            <Age>17</Age>
            <Gender>female</Gender>
        </Student>
        <Student ID_Code="ID--8">
            <Name>ABC--8</Name>
            <Age>18</Age>
            <Gender>male</Gender>
        </Student>
        <Student ID_Code="ID--9">
            <Name>ABC--9</Name>
            <Age>19</Age>
            <Gender>female</Gender>
        </Student>
    </Student_All_Infomation>
</Students_Infomation>
  • 解析结果:

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐