下载地址:TinyXML
解压缩TinyXML后,找到六个文件:
tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp
将这六个文件放在你的C++工程中,跟你的源文件在同一目录下。
新建一个XML文件,命名为Students.xml :
<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>
C++文件包含两个头文件:#include "tinyxml.h" 和 #include "tinystr.h" 便可使用TinyXMl了
#include "tinyxml.h" #include "tinystr.h" #include <iostream> #include <string> 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(); } system("pause"); return 0; }
利用TinyXML解析XML文件,布布扣,bubuko.com
原文地址:http://blog.csdn.net/gateway6143/article/details/26510137