使用Pugixml库,轻松处理XML文件
提示:知识积累
在这里插入代码片
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <pugiconfig.hpp> //包含头文件
#include <pugixml.hpp> //包含头文件
using namespace std;
using namespace pugi;
int main()
{
// 创建一个XML文档对象
xml_document doc;
// 添加根节点
xml_node root = doc.append_child("fruits");
// 添加子节点
xml_node apple = root.append_child("fruit");
apple.append_attribute("name").set_value("苹果");
apple.append_attribute("color").set_value("红色");
apple.append_attribute("taste").set_value("甜");
xml_node banana = root.append_child("fruit");
banana.append_attribute("name").set_value("香蕉");
banana.append_attribute("color").set_value("黄色");
banana.append_attribute("taste").set_value("甜");
// 将XML内容写入文件
ofstream file("fruits.xml");
doc.save(file);
file.close();
std::cin.get();
return 0;
}