TinyXML解析XML

TinyXML是个解析库,主要由DOM模型类(TiXmlBase、TiXmlNode、TiXmlAttribute、TiXmlComment、TiXmlDeclaration、TiXmlElement、TiXmlText、TiXmlUnknown)和操作类(TiXmlHandler)构成。它由两个头文件(.h文件)和四个CPP文件(.cpp文件)构成,用的时候,只要将(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)导入工程就可以用它的东西了。如果需要,可以将它做成自己的DLL来调用。举个例子就可以说明一切。

#include <iostream>
#include "tinyxml.h"
#include "tinystr.h"
#include <string>
#include <windows.h>
#include <atlstr.h>

using namespace std;

/*
*功能:获取应用程序根目录
*/
CString GetAppPath()
{
	TCHAR _ModulePath[MAX_PATH];
	GetModuleFileName(NULL,_ModulePath,MAX_PATH);
	CString _StrModulePath(_ModulePath);
	_StrModulePath=_StrModulePath.Left(_StrModulePath.ReverseFind(_T('\\')));
	return _StrModulePath;
}

/*
 *功能:创建XML文件
 *_FileName:文件保存路径,创建成功返回true,否则false
*/
bool CreateXmlFile(string& _FileName)
{
	try
	{
		TiXmlDocument *_MyDocument=new TiXmlDocument();  //创建一个XML对象
		TiXmlElement *_RootElement=new TiXmlElement("程序员");
		_MyDocument->LinkEndChild(_RootElement);

		TiXmlElement *_Programmer=new TiXmlElement("咆哮的蛋蛋");//创建咆哮的蛋蛋元素并连接
		_RootElement->LinkEndChild(_Programmer);

		_Programmer->SetAttribute("ID","001");//设置咆哮的蛋蛋的属性
		TiXmlElement *_SexElement=new TiXmlElement("sex");//咆哮的蛋蛋的性别
		TiXmlElement *_AgeElement=new TiXmlElement("age");//咆哮的蛋蛋的年龄
		_Programmer->LinkEndChild(_SexElement);
		_Programmer->LinkEndChild(_AgeElement);

		//设置name和age的元素内容并连接
		TiXmlText *_Sex=new TiXmlText("男");
		TiXmlText *_Age=new TiXmlText("22");
		_SexElement->LinkEndChild(_Sex);
		_AgeElement->LinkEndChild(_Age);
		CString _AppPath=GetAppPath();
		string _seperator="\\";
		string fullPath=_AppPath.GetBuffer(0)+_seperator+_FileName;
		_MyDocument->SaveFile(fullPath.c_str());//保存文件
	}
	catch(string& e)
	{
		return false;
	}
	return true;
}

/*
 *功能:读取XML文件并遍历
*/
bool ReadXmlFile(string& _FileName)
{
	try
	{
		CString _AppPath=GetAppPath();
		string _seperator="\\";
		string _fullPath=_AppPath.GetBuffer(0)+_seperator+_FileName;
		TiXmlDocument* _MyDocument=new TiXmlDocument(_fullPath.c_str());
		_MyDocument->LoadFile();

		TiXmlElement* _RootElement=_MyDocument->RootElement();//获得根元素,程序员
		cout<<_RootElement->Value()<<endl;

		TiXmlElement* _FirstElement=_RootElement->FirstChildElement();//获得第一个咆哮的蛋蛋节点

		TiXmlElement* _Sex=_FirstElement->FirstChildElement();//获得第一个咆哮的蛋蛋的sex节点和age节点和ID属性
		TiXmlElement* _Age=_Sex->NextSiblingElement();
		TiXmlAttribute* ID=_FirstElement->FirstAttribute();

		cout<<_Sex->FirstChild()->Value()<<endl;
		cout<<_Age->FirstChild()->Value()<<endl;
		cout<<ID->Value()<<endl;
	}
	catch(string& e)
	{
		return false;
	}
	return true;
}

int main()
{
	string _fileName="Demo.xml";
	CreateXmlFile(_fileName);
	ReadXmlFile(_fileName);
	system( "pause ");
}
生成的XML文件如图:


输出的结果为:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值