TinyXML使用

一. 下载编译

到官网下载tinyxml_2_6_2.zip http://www.grinninglizard.com/tinyxml/ 

现在出了TinyXML2, 那里说2的效率会高一点

http://www.grinninglizard.com/tinyxml2/index.html

我下载的是tinyxml_2_6_2 编译需要VC2010, 由于我用的版本比2010所以我自己重新创建一个静态库工程编译不使用VC2010编译.

编译步骤:

A. 使用VC2008/2005 创建一个Win32静态库工程TinyXML中的tinystr.cpp;tinystr.h;tinyxml.cpp;tinyxml.h;tinyxmlerror.cpp;tinyxmlparser.cpp 6个文件添加到静态库编程按提示修改错误(预编译头错误). 完成即可.


二. 最简单的使用

你懂的

  1. #include "stdafx.h"   
  2.   
  3. #include "../TinyXML/tinyxml.h"   
  4. #include "../TinyXML/tinystr.h"   
  5.   
  6. #include <iostream>   
  7. #include <string>   
  8. #pragma comment(lib,"../Debug/tinyxml.lib");   
  9.   
  10. bool CreateXMLFile(std::string& strFileName)  
  11. {  
  12.     try  
  13.     {  
  14.         // 创建一个XML的文档对象。   
  15.         TiXmlDocument *xmlDocument = new TiXmlDocument();  
  16.         // 根元素。   
  17.         TiXmlElement *rootElement = new TiXmlElement("Books");  
  18.         // 连接到文档对象, 作为根元素   
  19.         xmlDocument->LinkEndChild(rootElement);  
  20.         // 创建一个Book元素.   
  21.         TiXmlElement *bookElement = new TiXmlElement("Book");  
  22.         // 连接到根元素, 就是根元素的子元素   
  23.         rootElement->LinkEndChild(bookElement);  
  24.         // 设置Book元素的属性, 这里是ID。   
  25.         bookElement->SetAttribute("ID""1");  
  26.         // 创建Name元素和Price元素   
  27.         TiXmlElement *nameElement = new TiXmlElement("Name");  
  28.         TiXmlElement *priceElement = new TiXmlElement("Price");  
  29.         // 连接   
  30.         bookElement->LinkEndChild(nameElement);  
  31.         bookElement->LinkEndChild(priceElement);  
  32.         // 设置Name元素和Price元素的值。   
  33.         TiXmlText *nameValue = new TiXmlText("葵花宝典");  
  34.         nameElement->LinkEndChild(nameValue);  
  35.         TiXmlText *priceValue = new TiXmlText("50.00");  
  36.         priceElement->LinkEndChild(priceValue);  
  37.         xmlDocument->SaveFile(strFileName.c_str());  // 保存到文件   
  38.     }  
  39.     catch(...)  
  40.     {  
  41.         return false;  
  42.     }  
  43.     return true;  
  44. }  
  45.   
  46. bool ReadXMLFile(std::string& szFileName)  
  47. {  
  48.     try  
  49.     {  
  50.         // 创建一个XML的文档对象。   
  51.         TiXmlDocument *xmlDocument = new TiXmlDocument(szFileName.c_str());  
  52.         // 解析   
  53.         xmlDocument->LoadFile();  
  54.         // 获得根元素   
  55.         TiXmlElement *rootElement = xmlDocument->RootElement();  
  56.   
  57.         // 输出根元素名称   
  58.         std::cout << rootElement->Value() << std::endl;  
  59.         // 获得第一个节点。   
  60.         TiXmlElement *firstElement = rootElement->FirstChildElement();  
  61.         // 获得第一个Person的name节点和age节点和ID属性。   
  62.         TiXmlElement *nameElement = firstElement->FirstChildElement();  
  63.         TiXmlElement *priceElement = nameElement->NextSiblingElement();  
  64.         TiXmlAttribute *IDAttribute = firstElement->FirstAttribute();  
  65.   
  66.         // 输出   
  67.         std::cout << nameElement->FirstChild()->Value() << std::endl;  
  68.         std::cout << priceElement->FirstChild()->Value() << std::endl;  
  69.         std::cout << IDAttribute->Value()<< std::endl;  
  70.     }  
  71.     catch (...)  
  72.     {  
  73.         return false;  
  74.     }  
  75.     return true;  
  76. }  
  77.   
  78.   
  79. int _tmain(int argc, _TCHAR* argv[])  
  80. {  
  81.     std::string fileName = "C:\\test.xml";  
  82.     CreateXMLFile(fileName);  
  83.     ReadXMLFile(fileName);  
  84.     return 0;  
  85. }  
#include "stdafx.h"

#include "../TinyXML/tinyxml.h"
#include "../TinyXML/tinystr.h"

#include <iostream>
#include <string>
#pragma comment(lib,"../Debug/tinyxml.lib");

bool CreateXMLFile(std::string& strFileName)
{
	try
	{
		// 创建一个XML的文档对象。
		TiXmlDocument *xmlDocument = new TiXmlDocument();
		// 根元素。
		TiXmlElement *rootElement = new TiXmlElement("Books");
		// 连接到文档对象, 作为根元素
		xmlDocument->LinkEndChild(rootElement);
		// 创建一个Book元素.
		TiXmlElement *bookElement = new TiXmlElement("Book");
		// 连接到根元素, 就是根元素的子元素
		rootElement->LinkEndChild(bookElement);
		// 设置Book元素的属性, 这里是ID。
		bookElement->SetAttribute("ID", "1");
		// 创建Name元素和Price元素
		TiXmlElement *nameElement = new TiXmlElement("Name");
		TiXmlElement *priceElement = new TiXmlElement("Price");
		// 连接
		bookElement->LinkEndChild(nameElement);
		bookElement->LinkEndChild(priceElement);
		// 设置Name元素和Price元素的值。
		TiXmlText *nameValue = new TiXmlText("葵花宝典");
		nameElement->LinkEndChild(nameValue);
		TiXmlText *priceValue = new TiXmlText("50.00");
		priceElement->LinkEndChild(priceValue);
		xmlDocument->SaveFile(strFileName.c_str());	// 保存到文件
	}
	catch(...)
	{
		return false;
	}
	return true;
}

bool ReadXMLFile(std::string& szFileName)
{
	try
	{
		// 创建一个XML的文档对象。
		TiXmlDocument *xmlDocument = new TiXmlDocument(szFileName.c_str());
		// 解析
		xmlDocument->LoadFile();
		// 获得根元素
		TiXmlElement *rootElement = xmlDocument->RootElement();

		// 输出根元素名称
		std::cout << rootElement->Value() << std::endl;
		// 获得第一个节点。
		TiXmlElement *firstElement = rootElement->FirstChildElement();
		// 获得第一个Person的name节点和age节点和ID属性。
		TiXmlElement *nameElement = firstElement->FirstChildElement();
		TiXmlElement *priceElement = nameElement->NextSiblingElement();
		TiXmlAttribute *IDAttribute = firstElement->FirstAttribute();

		// 输出
		std::cout << nameElement->FirstChild()->Value() << std::endl;
		std::cout << priceElement->FirstChild()->Value() << std::endl;
		std::cout << IDAttribute->Value()<< std::endl;
	}
	catch (...)
	{
		return false;
	}
	return true;
}


int _tmain(int argc, _TCHAR* argv[])
{
	std::string fileName = "C:\\test.xml";
	CreateXMLFile(fileName);
	ReadXMLFile(fileName);
	return 0;
}


资源下载地址:

http://download.csdn.net/detail/cay22/5088811

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值