@H_404_0@例子如下:
#include "tinyxml.h" #include <iostream> #include <string> using namespace std; int main() { TiXmlDocument* myDocument = new TiXmlDocument(); myDocument->LoadFile("Students.xml"); TiXmlElement* rootElement = myDocument->RootElement(); //Class 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" << " : " << phoneElement->GetText() << std::endl; studentElement = studentElement->NextSiblingElement(); } return 0; }
@H_404_0@自己接着就写了个解析单词的例子:
<wordbook> <item> <word>gradual</word> <trans>a.逐渐的,逐步的</trans> </item> <item> <word>deceive</word> <trans>v.欺骗,蒙蔽</trans> </item> </wordbook>
程序:
#include "tinyxml.h" #include <iostream> #include <string> using namespace std; int main() { TiXmlDocument* myDocument = new TiXmlDocument(); myDocument->LoadFile("words.xml"); TiXmlElement* wordbook = myDocument->RootElement(); //wordbook TiXmlElement* items = wordbook->FirstChildElement();//item while ( items ) { TiXmlElement* word = items->FirstChildElement(); std::cout << word->GetText() << std::endl; TiXmlElement* trans = word->NextSiblingElement(); std::cout << trans->GetText() << std::endl; items = items->NextSiblingElement(); } return 0; }
@H_404_0@以上例子完全通过测试,所以,感觉这个类库很好用的,呵呵