参考链接
https://www.cnblogs.com/kex1n/archive/2010/10/03/1841502.html
https://blog.csdn.net/bjwzhscs/article/details/36873707
https://www.cnblogs.com/cy568searchx/p/3670400.html
本文使用的是tinyxml2库
#include "tinyxml2/tinyxml2.h"
#include <iostream>
#include <time.h>
bool creatXML() {
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
tinyxml2::XMLDeclaration* declaration = doc->NewDeclaration("xml version=\"1.0\"encoding=\"utf-8\"");
doc->LinkEndChild(declaration);
tinyxml2::XMLElement* eleSchool = doc->NewElement("School");
doc->LinkEndChild(eleSchool);
eleSchool->SetAttribute("name","软件学院");
tinyxml2::XMLElement* eleClass = doc->NewElement("Class");
eleSchool->LinkEndChild(eleClass);
eleClass->SetAttribute("name", "c++");
tinyxml2::XMLElement *eleStudent = doc->NewElement("Student");
eleClass->LinkEndChild(eleStudent);
eleStudent->SetAttribute("name", "tinyxml");
eleStudent->SetAttribute("number", 123);
tinyxml2::XMLElement *eleEmail = doc->NewElement("email");
eleStudent->LinkEndChild(eleEmail);
tinyxml2::XMLText *texEmail = doc->NewText("tinyxml@163.com");
eleEmail->LinkEndChild(texEmail);
tinyxml2::XMLElement *eleAddress = doc->NewElement("address");
eleStudent->LinkEndChild(eleAddress);
tinyxml2::XMLText *texAddress = doc->NewText("中国");
eleAddress->LinkEndChild(texAddress);
tinyxml2::XMLElement *eleStudent1 = doc->NewElement("Student");
eleClass->LinkEndChild(eleStudent1);
eleStudent1->SetAttribute("name", "jsoncpp");
eleStudent1->SetAttribute("number", 456);
tinyxml2::XMLElement *eleEmail1 = doc->NewElement("email");
eleStudent1->LinkEndChild(eleEmail1);
tinyxml2::XMLText *texEmail1 = doc->NewText("jsoncpp@gmail.com");
eleEmail1->LinkEndChild(texEmail1);
tinyxml2::XMLElement *eleAddress1 = doc->NewElement("address");
eleStudent1->LinkEndChild(eleAddress1);
tinyxml2::XMLText *texAddress1 = doc->NewText("美国");
eleAddress1->LinkEndChild(texAddress1);
if (tinyxml2::XML_SUCCESS != doc->SaveFile("school.xml")) {
delete doc;
return false;
}
doc->SaveFile("e:/tmp.xml");
delete doc;
return true;
}
bool ReadXML()
{
tinyxml2::XMLDocument doc;
if (tinyxml2::XML_SUCCESS != doc.LoadFile("school.xml"))
{
return false;
}
tinyxml2::XMLElement* eleSchool = doc.RootElement();
if (eleSchool)
{
std::cout << "School:"<<eleSchool->Attribute("name")<< std::endl;
tinyxml2::XMLElement* eleClass = eleSchool->FirstChildElement("Class");
while (eleClass)
{
std::cout << " Class "<< eleClass->Attribute("name")<< std::endl;
tinyxml2::XMLElement* eleStudent = eleClass->FirstChildElement("Student");
while (eleStudent)
{
std::cout << " Student:" << eleStudent->Attribute("name") << ", " << eleStudent->Attribute("number") << std::endl;
tinyxml2::XMLElement* eleEmail = eleStudent->FirstChildElement("email");
if (eleEmail)
{
std::cout << " Email:" << eleEmail->GetText() << std::endl;
}
tinyxml2::XMLElement* eleAddress = eleStudent->FirstChildElement("address");
if (eleAddress)
{
std::cout << " Address:" << eleAddress->GetText() << std::endl;
}
eleStudent = eleStudent->NextSiblingElement("Student");
}
eleClass = eleClass->NextSiblingElement();
}
}
}
int main()
{
tinyxml2::XMLDocument doc;
doc.LoadFile("school.xml");
doc.Print();
system("pause");
}