参考:
tinyxml官网:
官方文档:
github上文件下载地址:
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:
miaomaio Shaanxi Xi'an 13759911917 miaomiao@home.com gougou Liaoning Shenyang 15840330481 gougou@home.com
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.\n", filepath,doc.ErrorDesc() ); exit( 1 ); } // get dom root of 'phonebookdata.xml', here root should be 'phonebook'. TiXmlElement* root = doc.RootElement(); printf("_______________________________________\n\n"); printf(" contacted person information \n\n"); // trace every items below root. for( TiXmlNode* item = root->FirstChild( "item" ); item; item = item->NextSibling( "item" ) ) { printf("_______________________________________\n"); // read name. TiXmlNode* child = item->FirstChild(); const char* name = child->ToElement()->GetText(); if (name) { printf("name:%s\n",name); } else { printf("name:\n"); } // read address. child = item->IterateChildren(child); const char* addr = child->ToElement()->GetText(); if (addr) { printf("addr:%s\n",addr); } else { printf("addr:\n"); } // read telephone no. child = item->IterateChildren(child); const char* tel = child->ToElement()->GetText(); if (tel) { printf("tel:%s\n",tel); } else { printf("tel:\n"); } // read e-mail. child = item->IterateChildren(child); const char* email = child->ToElement()->GetText(); if(email) { printf("email:%s\n",email); } else { printf("email:\n"); } printf("\n"); } //______________________________________________________________________ //______________________________________________________________________ // 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(); //______________________________________________________________________