编程杂谈——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;
}
posted @ 2019-10-12 19:56  Ken.W  阅读(1155)  评论(0编辑  收藏  举报