timyxml的简单使用
#include <iostream> #include <string> #include <cstdio> #include "tinyxml.h" using namespace std; int main() { TiXmlPrinter printer; string filepath = "test.xml"; TiXmlDocument doc(filepath.c_str()); bool loadOkay = doc.LoadFile(); if (!loadOkay) { printf( "Could not load test file %s. Error='%s'. Exiting.\n", filepath,doc.ErrorDesc() ); exit( 1 ); } doc.Accept(&printer); cout << printer.CStr() << std::endl; TiXmlElement* root = doc.RootElement(); for( TiXmlNode* item = root->FirstChild("item"); item; item = item->NextSibling("item")) { printf("_______________________________________\n"); TiXmlNode* child = item->FirstChild(); string name = child->ToElement()->GetText(); if (!name.empty()) { cout << "name = " << name << endl; } child = item->IterateChildren(child); string addr = child->ToElement()->GetText(); if (!addr.empty()) { cout << "address = " << name << endl; } child = item->IterateChildren(child); string tel = child->ToElement()->GetText(); if (!tel.empty()) { cout << "tel = " << tel << endl; } child = item->IterateChildren(child); string email = child->ToElement()->GetText(); if(!email.empty()) { cout << "email = " << email << endl; } cout << endl; } return 0; }
test.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.--> <item> <name>pipi</name> <addr>Shaanxi Xianyang</addr> <tel>02937310627</tel> <email>pipi@home.com</email> </item> </phonebook>
输出:
<?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.--> <item> <name>pipi</name> <addr>Shaanxi Xianyang</addr> <tel>02937310627</tel> <email>pipi@home.com</email> </item> </phonebook> _______________________________________ name = miaomaio address = miaomaio tel = 13759911917 email = miaomiao@home.com _______________________________________ name = gougou address = gougou tel = 15840330481 email = gougou@home.com _______________________________________ name = pipi address = pipi tel = 02937310627 email = pipi@home.com 请按任意键继续. . .
<?xml version="1.0" encoding="GB2312" ?> <Class name="计算机软件班"> <Students> <student name="张三" studentNo="13031001" sex="男" age="22"> <phone>88208888</phone> <address>西安市太白南路二号</address> </student> <student name="李四" studentNo="13031002" sex="男" age="20"> <phone>88206666</phone> <address>西安市光华路</address> </student> </Students> </Class>
#include <iostream> #include <string> #include "tinyxml.h" #pragma comment(lib, "tinyxml.lib") using std::string; int main() { TiXmlPrinter printer; TiXmlDocument* myDocument = new TiXmlDocument(); myDocument->LoadFile("test.xml"); myDocument->Accept(&printer); //std::cout << printer.CStr() << std::endl; TiXmlElement* rootElement = myDocument->RootElement(); //Class std::cout << rootElement->Value() << ": "; std::cout << rootElement->FirstAttribute()->Value() << std::endl; std ::cout << "-----------------------------------------------" << std::endl; TiXmlElement* studentsElement = rootElement->FirstChildElement(); //Students TiXmlElement* studentElement = studentsElement->FirstChildElement(); //Student while ( studentElement ) { TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute(); //获得student的name属性 while ( attributeOfStudent ) { std::cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value() << std::endl; attributeOfStudent = attributeOfStudent->Next(); } TiXmlElement* phoneElement = studentElement->FirstChildElement();//获得student的phone元素 std::cout << "phone" << " : " << phoneElement->GetText() << std::endl; TiXmlElement* addressElement = phoneElement->NextSiblingElement(); std::cout << "address" << " : " << addressElement->GetText() << std::endl; std ::cout << "-----------------------------------------------" << std::endl; studentElement = studentElement->NextSiblingElement(); } return 0; }
输出:
Class: 计算机软件班 ----------------------------------------------- name : 张三 studentNo : 13031001 sex : 男 age : 22 phone : 88208888 address : 西安市太白南路二号 ----------------------------------------------- name : 李四 studentNo : 13031002 sex : 男 age : 20 phone : 88206666 address : 西安市光华路 ----------------------------------------------- 请按任意键继续. . .
Keep it simple!