利用TingXML读XML文件
①待读取的xml文件
②程序
#include"tinyxml2.h"
#include<iostream>
using namespace tinyxml2;
using namespace std;
//<?xml version="1.0" encoding="utf-8"?>
//<!-- This is a XML comment! -->
//<note>
// <program type="c++">
// <title language="english" id="12138">Read XML</title>
// <author>Jay</author>
// <time>2019</time>
// </program>
//</note>
int main()
{
XMLDocument *doc=new XMLDocument;
doc->LoadFile("write.xml");
auto pNote=doc->FirstChildElement();
cout<<pNote->Name()<<endl;
auto pProgram=pNote->FirstChildElement();
cout<<pProgram->Name()<<":";
cout<<pProgram->FirstAttribute()->Name()<<" ";
cout<<pProgram->FirstAttribute()->Value()<<endl;
auto pTitle=pProgram->FirstChildElement();
while(pTitle)
{
cout<<pTitle->Name()<<":"<<pTitle->GetText()<<"\t";
auto pTitleAtrr=pTitle->FirstAttribute();
while(pTitleAtrr)//通过循环得到属性
{
cout<<pTitleAtrr->Name()<<":"<<pTitleAtrr->Value()<<" ";
pTitleAtrr=pTitleAtrr->Next();
}
cout<<endl;
pTitle=pTitle->NextSiblingElement();
}
delete doc;
return 0;
}
③效果