使用TinyXML2进行xml文件操作

  • 简介

TinyXML2适用于大部分的C/C++项目开发,经得住考验,是最好的选择。较TinyXML1而言,TinyXML2化繁为简,使用时只需要包含两个文件,而TinyXML1需要6个文件,一般生成静态链接库供项目的使用。它使用更少的内存,更快,并使用更少的内存分配。

TinyXML2的优点: (1)对大部分大部分的C/C++项目具有普适性。 (2)使用较少的内存(约TinyXML1的40%),速度变得更快。 (3)没有C++的STL的要求。 (4)更接近现代C++的特性,如使用了适当的命名空间。 (5)适当有效的处理了的空白字符(空格,TAB和回车)。

TinyXML1的优点: (1)可以报告分析错误的位置。 (2)提供一些C++ STL公约支持:流和字符串。 (3)拥有非常成熟和良好的调试代码库。

TinyXML2官网地址:http://www.grinninglizard.com/tinyxml2/index.html

#include "tinyxml2.h"
#include 


using namespace tinyxml2;
using namespace std;

bool createXml(const char* savepath)
{
	XMLDocument *doc = new XMLDocument;
	XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"utf-8\"");
	doc->LinkEndChild(declaration);

	XMLElement *Country = doc->NewElement("Country");
	doc->LinkEndChild(Country);
	Country->SetAttribute("name", "China");

	XMLElement *City = doc->NewElement("City");
	Country->LinkEndChild(City);
	City->SetAttribute("name", "BeiJing");

	XMLElement *People = doc->NewElement("People");
	City->LinkEndChild(People);
	People->SetAttribute("name", "张三");
	People->SetAttribute("number", "01");

	XMLElement *Gender = doc->NewElement("Gender");	
	XMLText * gender = doc->NewText("Man");
	Gender->LinkEndChild(gender);
	
	XMLElement *Address = doc->NewElement("Address");
	XMLText * address = doc->NewText("朝阳");
	Address->LinkEndChild(address);

	People->LinkEndChild(Gender);
	People->LinkEndChild(Address);

	People = doc->NewElement("People");
	City->LinkEndChild(People);
	People->SetAttribute("name", "李四");
	People->SetAttribute("number", "02");

	Gender = doc->NewElement("Gender");
	gender = doc->NewText("Woman");
	Gender->LinkEndChild(gender);

	Address = doc->NewElement("Address");
	address = doc->NewText("海淀");
	Address->LinkEndChild(address);

	People->LinkEndChild(Gender);
	People->LinkEndChild(Address);

	if (XML_SUCCESS != doc->SaveFile(savepath)) 
	{
		delete doc;
		return false;
	}

	delete doc;
	return true;
}
bool readXml(const char* filename)
{
	XMLDocument doc;
	if (XML_SUCCESS != doc.LoadFile(filename)) 
	{
		return false;
	}

	XMLElement *Country = doc.RootElement();
	if (Country)
	{
		cout << "School:" << Country->Attribute("name") << endl;

		XMLElement *City = Country->FirstChildElement("City");
		while (City)
		{
			cout << "City:" << City->Attribute("name") << endl;

			XMLElement *People = City->FirstChildElement("People");
			while (People)
			{
				cout << "People:" << People->Attribute("name") << ", " << People->Attribute("number") << endl;

				XMLElement* Gender = People->FirstChildElement("Gender");
				if (Gender)
				{
					cout << "Gender:" << Gender->GetText() << endl;
				}
				XMLElement* Address = People->FirstChildElement("Address");
				if (Address) 
				{
					cout << "Address:" << Address->GetText() << endl;
				}
				People = People->NextSiblingElement("People");
			}

			City = City->NextSiblingElement();
		}

	}
}

main函数

int main()
{
	char *path = "tmp.xml";
	createXml(path);
	readXml(path);
	return 0;
}

 

你可能感兴趣的:(c++学习)