tinyxml入门

网上找的列子

 

 

从http://www.grinninglizard.com/tinyxml/index.html  下载tinyxml

 

学习资料http://www.cnblogs.com/clever101/archive/2010/04/05/1704876.html

 

复制tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp   6个文件到工程

 

xml文件

 

<?xml version="1.0" standalone = "yes/no" encoding="UTF-8"?> 

<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 <string>
#include "tinyxml.h"
using namespace std;

int main()
{
		TiXmlDocument* myDocument = new TiXmlDocument();
		myDocument->LoadFile("/root/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" << " : " << addressElement->GetText()
								<< std::endl;
				studentElement = studentElement->NextSiblingElement();
		}
		return 0;
}


编译运行OK

 

你可能感兴趣的:(tinyxml入门)