TinyXML2 使用

匿名 (未验证) 提交于 2019-12-02 23:59:01

TinyXML2

TinyXML2

Demo

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 //  #include "stdafx.h" #include "tinyxml2.h" #include <iostream>   using namespace tinyxml2; using namespace std;  bool createXml(const char* savepath) {     XMLDocument *doc = new XMLDocument;     XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");     doc->LinkEndChild(declaration);      XMLElement *School = doc->NewElement("School");     doc->LinkEndChild(School);     School->SetAttribute("name","机械工程学院");      XMLElement *Class = doc->NewElement("Class");     School->LinkEndChild(Class);     Class->SetAttribute("name","c++");      XMLElement *Student = doc->NewElement("Student");     Class->LinkEndChild(Student);     Student->SetAttribute("name","天霸");     Student->SetAttribute("number", "01");      XMLElement *Email = doc->NewElement("Email");     Student->LinkEndChild(Email);     XMLText * email = doc->NewText("TB@126.com");     Email->LinkEndChild(email);      XMLElement *Address = doc->NewElement("Address");     Student->LinkEndChild(Address);     XMLText * address = doc->NewText("中国辽宁");     Address->LinkEndChild(address);      XMLElement *Student_1 = doc->NewElement("Student");     Class->LinkEndChild(Student_1);     Student_1->SetAttribute("name", "动霸");     Student_1->SetAttribute("number", "02");      XMLElement *Email_1 = doc->NewElement("Email");     Student_1->LinkEndChild(Email_1);     XMLText * email_1 = doc->NewText("DB@126.com");     Email_1->LinkEndChild(email_1);      XMLElement *Address_1 = doc->NewElement("Address");     Student_1->LinkEndChild(Address_1);     XMLText * address_1 = doc->NewText("中国香港");     Address_1->LinkEndChild(address_1);       if (XML_SUCCESS != doc->SaveFile(savepath)) {         delete doc;         return false;     }      delete doc;     return true; }  bool readXml(const char* filename) {     XMLDocument doc;     if (XML_SUCCESS != doc.LoadFile(filename)) {         return false;     }      XMLElement *School = doc.RootElement();     if (School){         cout << "School:" << School->Attribute("name") << endl;          XMLElement *Class = School->FirstChildElement("Class");         while (Class) {             cout << "   Class:" << Class->Attribute("name") << endl;              XMLElement *Student = Class->FirstChildElement("Student");             while (Student) {                 cout << "       Student:" << Student->Attribute("name") << ", " << Student->Attribute("number") << endl;                  XMLElement* Email = Student->FirstChildElement("email");                 if (Email){                     cout << "       Email:" << Student->GetText() << endl;                 }                 XMLElement* Address = Student->FirstChildElement("address");                 if (Address){                     cout << "       Address:" << Address->GetText() << endl;                 }                 Student = Student->NextSiblingElement("Student");             }              Class = Class->NextSiblingElement();         }      } }   int main() {     char * path = "d:/tmp.xml";     createXml(path);     readXml(path);     return 0; }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!