参考:http://blog.csdn.net/L_Andy/article/details/40615517
tinyxml官网:
http://www.grinninglizard.com/tinyxml/
官方文档:
http://www.grinninglizard.com/tinyxmldocs/index.html
github上文件下载地址:
https://sourceforge.net/projects/tinyxml/
TinyXML类:
TiXmlBase:整个TinyXML模型的基类。
TiXmlAttribute:对应于XML中的元素的属性。
TiXmlNode:对应于DOM结构中的节点。
TiXmlComment:对应于XML中的注释。<!--XXXXX.-->
TiXmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。
TiXmlDocument:对应于XML的整个文档。
TiXmlElement:对应于XML的元素。
TiXmlText:对应于XML的文字部分。
TiXmlUnknown:对应于XML的未知部分。
TiXmlHandler:定义了针对XML的一些操作。
使用:
XML:
<?xml version="1.0" encoding="UTF-8"?> <phonebook> <!--one item behalfs one contacted person.--> <item> <name>miaomaio</name> <addr>Shaanxi Xi'an</addr> <tel>13759911917</tel> <email>miaomiao@home.com</email> </item> <item> <name>gougou</name> <addr>Liaoning Shenyang</addr> <tel>15840330481</tel> <email>gougou@home.com</email> </item> <!--more contacted persons.--> </phonebook>
C++:
//______________________________________________________________________ // Read information from xml file. // define xml file path, as follow , we use relative path, // but you can use absolute path also. const char* filepath = "phonebookdata.xml"; TiXmlDocument doc(filepath); bool loadOkay = doc.LoadFile(); // faile to load 'phonebookdata.xml'. if (!loadOkay) { printf( "Could not load test file %s. Error='%s'. Exiting. ", filepath,doc.ErrorDesc() ); exit( 1 ); } // get dom root of 'phonebookdata.xml', here root should be 'phonebook'. TiXmlElement* root = doc.RootElement(); printf("_______________________________________ "); printf(" contacted person information "); // trace every items below root. for( TiXmlNode* item = root->FirstChild( "item" ); item; item = item->NextSibling( "item" ) ) { printf("_______________________________________ "); // read name. TiXmlNode* child = item->FirstChild(); const char* name = child->ToElement()->GetText(); if (name) { printf("name:%s ",name); } else { printf("name: "); } // read address. child = item->IterateChildren(child); const char* addr = child->ToElement()->GetText(); if (addr) { printf("addr:%s ",addr); } else { printf("addr: "); } // read telephone no. child = item->IterateChildren(child); const char* tel = child->ToElement()->GetText(); if (tel) { printf("tel:%s ",tel); } else { printf("tel: "); } // read e-mail. child = item->IterateChildren(child); const char* email = child->ToElement()->GetText(); if(email) { printf("email:%s ",email); } else { printf("email: "); } printf(" "); } //______________________________________________________________________ //______________________________________________________________________ // Add information to xml file and save it. TiXmlElement* writeRoot = doc.RootElement(); TiXmlNode* newNode = new TiXmlElement("item"); const TiXmlNode* name4NewNode = new TiXmlElement("name"); newNode->InsertEndChild(*name4NewNode)->InsertEndChild(TiXmlText("pipi")); const TiXmlNode* addr4NewNode = new TiXmlElement("addr"); newNode->InsertEndChild(*addr4NewNode)->InsertEndChild(TiXmlText("Shaanxi Xianyang")); const TiXmlNode* tel4NewNode = new TiXmlElement("tel"); newNode->InsertEndChild(*tel4NewNode)->InsertEndChild(TiXmlText("02937310627")); const TiXmlNode* email4NewNode = new TiXmlElement("email"); newNode->InsertEndChild(*email4NewNode)->InsertEndChild(TiXmlText("pipi@home.com")); writeRoot->InsertEndChild(*newNode); doc.SaveFile(); //______________________________________________________________________