編程雜談——TinyXML2的快速實踐


最近遇到個需要在C++中處理XML文件的需求,雖然對此方面並不是很熟,但好在有GitHub上的awesome-cpp項目的幫助,還是收獲了足夠的相關知識。

類庫

常用的或被推薦的XML類庫有以下數個選項,不過相較於純C完成的類庫個人還是更傾向於C++的類庫:

  • Boost.PropertyTree - A property tree parser/generator that can be used to parse XML/JSON/INI/Info files. [Boost]
  • Expat - An XML parser library written in C. [MIT]
  • Libxml2 - The XML C parser and toolkit of Gnome. [MIT]
  • libxml++ - An XML Parser for C++. [LGPL2]
  • Mini-XML - A small XML parsing library written in ANSI C. [LGPL2 with exceptions]
  • PugiXML - A light-weight, simple and fast XML parser for C++ with XPath support. [MIT]
  • RapidXml - An attempt to create the fastest XML parser possible, while retaining useability, portability and reasonable W3C compatibility. [Boost]
  • TinyXML - A simple, small, minimal, C++ XML parser that can be easily integrating into other programs. [zlib]
  • TinyXML2 - A simple, small, efficient, C++ XML parser that can be easily integrating into other programs. [zlib]
  • TinyXML++ - A completely new interface to TinyXML that uses MANY of the C++ strengths. Templates, exceptions, and much better error handling. [MIT]
  • Xerces-C++ - A validating XML parser written in a portable subset of C++. [Apache2]

TinyXML VS TinyXML2

TinyXML是在尋找更多信息時被多次提及的,因為並不想花費過多時間在做選擇題上,於是其似乎成了最終的贏家。

但未曾想它自身還有兩個版本。

TinyXML與TinyXML2的相同點:

  • 簡單的API
  • 基於DOM的解析器
  • 支持UTF-8 Unicode

TinyXML2的優點:

  • 着眼於未來的開發
  • 更少的內存分配(1/10到1/100),使用更少的內存(TinyXML的40%),更快(讀取上約5倍)
  • 不再需要STL
  • 更現代的C++,包括一個合適的命名空間
  • 適當且有用地處理空白

TinyXML的優點:

  • 能夠報告解析錯誤的位置
  • 支持一些C++ STL約定:流與字符串
  • 非常成熟並且調試良好的代碼庫

TinyXML2的第2及第4項優點是我更中意的,所以還是選它吧。

使用方法

在其GitHub的倉庫中下載相關文件,tinyxml2

找到tinyxml2.h與tinyxml2.cpp兩個文件,將它們添加至你的工程項目中,這便是所有需要的。

示例

#include <iostream>
#include <random>

#include "tinyxml2.h"
using namespace tinyxml2;

void writeXMLFile()
{
	XMLDocument doc;
	auto delaration = doc.NewDeclaration();
	doc.InsertFirstChild(delaration);

	auto root = doc.NewElement("root");
	doc.InsertEndChild(root);

	auto id = doc.NewElement("id");
	id->SetText(666);
	root->InsertEndChild(id);

	auto name = doc.NewElement("name");
	name->SetText("Ken");
	name->SetAttribute("blogger", true);
	root->InsertEndChild(name);

	doc.SaveFile("sample.xml");
}

XMLDocument* readXMLFile()
{
	auto doc = new XMLDocument;
	doc->LoadFile("sample.xml");

	auto root = doc->RootElement();
	auto id = root->FirstChildElement("id");
	
	std::cout << id->GetText() << std::endl;

	auto name = root->FirstChildElement("name");
	std::cout << name->GetText() << std::endl;
	std::cout << name->Attribute("blogger") << std::endl;

	return doc;
}

int main()
{
	writeXMLFile();
	auto doc = readXMLFile();
	auto root = doc->RootElement();
	auto id = root->FirstChildElement("id");
	doc->DeleteNode(id);

	auto randomid = doc->NewElement("randomid");
	
	std::default_random_engine e;
	std::uniform_int_distribution<int> u;
	auto r = u(e, decltype(u)::param_type(1000000, 9000000));

	randomid->SetText(r);
	root->InsertFirstChild(randomid);

	doc->Print();

	delete doc;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM