XML文件Students.xml如下:
88208888
西安市太白南路二号
88206666
西安市光华路
程序代码XmlParseExample.cpp如下所示:
#include
#include
#include
using std::string;
int main()
{
TiXmlDocument* myDocument = new TiXmlDocument();
myDocument->LoadFile("Students.xml");
TiXmlElement* rootElement = myDocument->RootElement(); //Class
TiXmlElement* studentsElement = rootElement->FirstChildElement(); //Students
TiXmlElement* studentElement = studentsElement->FirstChildElement(); //Students
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;
}
程序运行结果如下:
name : 张三
studentNo : 13031001
*** : 男
age : 22
phone : 88208888
address : 88208888
name : 李四
studentNo : 13031002
*** : 男
age : 20
phone : 88206666
阅读(1208) | 评论(0) | 转发(1) |