您的位置:首页 > 其它

你不再需要TinyXML,推荐RapidXML

2011-01-04 14:37 337 查看
目前我公司开发的Nexus
Engine的底层对象序列化使用了TinyXML来读写XML文件。TinyXML有两个不爽的地方,一是它的接口使用FILE*,另外一个是它对
wchar_t不能很好的支持。前阵子看Boost库的更新中多了一个PropertyTree,他在处理XML时用到了另外一个小的库
--RapidXML。既然间接的是Boost库的一部分,所以是值得一试的。于是找到其官方网站(http://rapidxml.sourceforge.net/

)研究了一番。一看之下,甚是满意,也推荐给大家看看!

首先就是速度,据它自己宣称比TinyXML快30到60倍



,比Xerces DOM快50到100倍!详细的测试比较请见其用户手册(http://rapidxml.sourceforge.net/manual.html

)的“4. Performance ”一节。

其次它的设计非常的简洁,只依赖于标准库中的几个基本的类。它的输入输出都是字符串,这样很好,一个库就应该关注自己核心的内容,做尽量少的事情。它的API其实和TinyXML倒是有几分相似,用过TinyXML的人应该很容易上手:

TinyXML主要接口类

RapidXML的主要接口类

class TiXmlDocumenttemplate

class xml_document

class TiXmlNodetemplate

class xml_node

class TiXmlAttribute template

class xml_attribute

下面还是看一个具体的例子来体验一下,下面是TinyXML官方教程中创建XML文档的一段代码:

view plain
copy to clipboard
print
?

void
build_simple_doc( )

{

// Make xml: World

TiXmlDocument doc;

TiXmlDeclaration * decl = new
TiXmlDeclaration(
"1.0"
,
""
,
""
);

TiXmlElement * element = new
TiXmlElement(
"Hello"
);

TiXmlText * text = new
TiXmlText(
"World"
);

element->LinkEndChild( text );

doc.LinkEndChild( decl );

doc.LinkEndChild( element );

doc.SaveFile( "madeByHand.xml"
);

}

void build_simple_doc( )
{
// Make xml: World
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
TiXmlElement * element = new TiXmlElement( "Hello" );
TiXmlText * text = new TiXmlText( "World" );
element->LinkEndChild( text );
doc.LinkEndChild( decl );
doc.LinkEndChild( element );
doc.SaveFile( "madeByHand.xml" );
}


下面是使用RapidXML实现类似功能的代码:

view plain
copy to clipboard
print
?

void
build_simple_doc_by_rapidxml()

{

xml_document<> doc;

xml_node<>* decl = doc.allocate_node(node_declaration);

xml_attribute<>* decl_ver =

doc.allocate_attribute("version"
,
"1.0"
);

decl->append_attribute(decl_ver);

doc.append_node(decl);

xml_node<>* node =

doc.allocate_node(node_element, "Hello"
,
"World"
);

doc.append_node(node);

string text;

rapidxml::print(std::back_inserter(text), doc, 0);

// write text to file by yourself

}

void build_simple_doc_by_rapidxml()
{
xml_document<> doc;
xml_node<>* decl = doc.allocate_node(node_declaration);
xml_attribute<>* decl_ver =
doc.allocate_attribute("version", "1.0");
decl->append_attribute(decl_ver);
doc.append_node(decl);
xml_node<>* node =
doc.allocate_node(node_element,    "Hello", "World");
doc.append_node(node);
string text;
rapidxml::print(std::back_inserter(text), doc, 0);
// write text to file by yourself
}


下面是使用RapidXML分析XML的样例代码:

view plain
copy to clipboard
print
?

void
parse_doc_by_rapidxml(
char
* xml_doc)

{

xml_document<> doc; // character type defaults to char

doc.parse<0>(xml_doc); // 0 means default parse flags

xml_node<> *node = doc.first_node("Hello"
);

string node_val = node->value();

}

void parse_doc_by_rapidxml(char* xml_doc)
{
xml_document<> doc;        // character type defaults to char
doc.parse<0>(xml_doc);  // 0 means default parse flags
xml_node<> *node = doc.first_node("Hello");
string node_val = node->value();
}


好东西,大家分享!:D
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: