常见C/C++ XML解析器比较

来源:互联网 发布:杨紫为什么土 知乎 编辑:程序博客网 时间:2024/05/19 15:21

常见C/C++ XML解析器有tinyxml、XERCES、squashxml、xmlite、pugxml、libxml等等,这些解析器有些是支持多语言的,有些只是单纯C/C++的。如果你是第一次接触到XML解析,那么关于XML解析器的选择问题的确令人头疼,我们先看一下这些解析器有哪些特点。

一、Xerces XML解析器

官方网址:http://xerces.apache.org/xerces-c/

Xerces前身是IBM的XML4C,XML4C也是一种功能强大的XML解析器,之后交给Apache基金会管理,遂改名为Xerces,Xerces-C++让你的程序提供读写XML数据更加容易,提供的共享库通过DOM、SAX、SAX2 API等方式对XML文档进行解析、生成、操作和验证。

Xerces-C++忠实于XML 1.0建议和相关标准。

Xerces-C++解析器高性能、模块化并且可扩展。相关开发资料也比较完善。

除了C++版本,Xerces同时还提供Xerces Java,Xerces Perl等版本。

二、TinyXML解析器

官方网址:http://www.grinninglizard.com/tinyxml/

TinyXML相比Xerces要功能简单些,正如其名Tiny,使用方法也比较简单,TinyXML也是一个开源的解析XML解析库,用于C++,支持Windows和Linux。TinyXML通过DOM模型遍历和分析XML。官方文档:

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

android code :

external/tinyxml//   C++

external/tinyxml2


expat解析器

android code :  external/expat     //  C 
http://www.libexpat.org/

       https://sourceforge.net/projects/expat/

Expat is a stream-oriented parser. You register callback (or handler) functions with the parser and then start feeding it the document. As the parser recognizes parts of the document, it will call the appropriate handler for that part (if you've registered one.) The document is fed to the parser in pieces, so you can start parsing before you have all the document. This also allows you to parse really huge documents that won't fit into memory.

Expat can be intimidating due to the many kinds of handlers and options you can set. But you only need to learn four functions in order to do 90% of what you'll want to do with it:

XML_ParserCreate
Create a new parser object.
XML_SetElementHandler
Set handlers for start and end tags.
XML_SetCharacterDataHandler
Set handler for text.
XML_Parse
Pass a buffer full of document to the parser

三、squashXML解析器

官方地址:http://ostatic.com/squashxml

这个解析器在国内似乎少人使用,这个解析器也有些历史了。squashXML基于DOM Level2,也是一个XML轻量级的解析器。天缘之所以把这个写上是天缘比较看重这个解析器的目录划分及使用说明,易懂而且易上手。

四、XMLBooster解析器

官方网址:http://www.xmlbooster.com/

XMLBooster开发关注点比较有特色,更加关注解析性能,声称:“Application integration of XML data cannot get any simpler or any faster: instead of dealing with sophisticated api (such as DOM or SAX), use a convenient data structure, generated to suit your specific purpose, in the language of your choice. ”。

针对特殊需求使用更加方便的数据结构以提高性能。

五、LibXML解析器

官方地址:http://xmlsoft.org/

LibXML本来是为Gnome项目开发(C开发),之后被广泛使用,功能非常强大,几乎适合于常见的所有操作系统下编译和开发使用。libxml++(地址:http://libxmlplusplus.sourceforge.net/)是对libxml XML解析器的C++封装版本。此外还有各种语言封装包,参加官方链接。

补充:

除了上述XML解析库外,还有一些XML解析器(参考:http://www.garshol.priv.no/xmltools/platform/cpp.html),比如Berkely DBXML(BDB)等,有兴趣的读者可自行Google搜索。

尽管XML解析器有很多种,而且功能差异很大,甚至是支持跨平台、多语言,但是对于你的应用而言,尽量选择一种相对熟悉、功能够用的即可,没必要去追求庞杂的解析器,我们只需关注:功能够用、相对稳定、适合扩展这三个功能即可。一旦有问题,修正和扩展都要更为容易。


原文地址:http://www.metsky.com/archives/578.html

0 0
原创粉丝点击