TinyXML-2 是一个轻量级的开源 C++ XML 解析库,用于处理XML数据。
#include"pch.h"
#include <iostream>
#include <string>
#include "tinyxml2.h"
//tinyxml2的类在tinyxml2命名空间
using namespace tinyxml2;
int main()
{
//0、创建对象
tinyxml2::XMLDocument doc;
//1、添加声明---XMLDeclaration*
XMLDeclaration* declaration = doc.NewDeclaration();
//2、创建根节点---XMLElement*
XMLElement* root = doc.NewElement("Root"); doc.InsertEndChild(root);
//3、创建子节点---XMLElement*
<User Name = "fengbingchun">
<Blog>CSDN< / Blog>
<Code>GitHub< / Code>
< / User>
XMLElement* user = doc.NewElement("User");
user->SetAttribute("Name", "feng");//user->Attribute("Name", "feng");
root->InsertEndChild(user);
XMLElement* blog = doc.NewElement("Blog");
XMLText* text1 = doc.NewText("CSDN");
blog->InsertEndChild(text1);
user->InsertEndChild(blog);
XMLElement* code = doc.NewElement("Code");
XMLText* text2 = doc.NewText("GtiHub");
code->InsertEndChild(text2);
user->InsertEndChild(code);
//<Blog Name = "CSDN">
// <Address>https://blog.csdn.net/fengbingchun</Address>
// <ID>fengbingchun< / ID>
//< / Blog>
XMLElement* blog2 = doc.NewElement("Blog");
blog2->SetAttribute("Name", "CSDN");//blog2->Attribute("Name", "CSDN");
root->InsertEndChild(blog2); //设置结束标签: 大节点-》小节点 (父节点-》子节点)
XMLElement* addr = doc.NewElement("Address");
XMLText* text3 = doc.NewText("https://blog.csdn.net/fengbingchun");
addr->InsertEndChild(text3);
blog2->InsertEndChild(addr); // 大节点-》小节点 (父节点-》子节点)
XMLElement* ID = doc.NewElement("ID");
XMLText* text4 = doc.NewText("feng");
ID->InsertEndChild(text4);
blog2->InsertEndChild(ID);
//<Code Name = "GitHub">
// <Address>https://github.com//fengbingchun</Address>
// < Repositories>27 < / Repositories >
//< / Code>
XMLElement* code2 = doc.NewElement("Code");
code2->SetAttribute("Name", "GitHub");
root->InsertEndChild(code2);
XMLElement* add2 = doc.NewElement("Address");
XMLText* text5 = doc.NewText("https://github.com//fengbingchun");
add2->InsertEndChild(text5);
code2->InsertEndChild(add2);
XMLElement* repositories = doc.NewElement("Repositories"