来源:http://blog.csdn.net/clever101
以前觉得tinyxml也是一个挺好的操作xml文件的库。最近找到了pugixml库,发现pugixml库对tinyxml可谓是全面胜出。
一.支持字符集:tinyxml不支持unicode(这个可谓是很多人不愿意用tinyxml的原因之一),pugixml支持UTF8 encoding、Little-endian UTF16、Big-endian UTF16、UTF16 with native endianness、Little-endianUTF32、Big-endian UTF32和UTF32with native endianness。
二.操作xml文件的性能。
Xml库解析性能比较表
(表格来自:http://rapidxml.sourceforge.net/manual.html)
看看上表吧,pugixml比tinyxml快不止一个数量级,仅比最快的RapidXml慢一点。pugixml比RapidXml的一个优点是pugixml支持xpath, RapidXml不支持xpath。
一.使用的方便性。虽然pugixml和tinyxml都是基于面向对象的,但pugixml的使用方便性远胜tinyxml。比如在查找节点的属性值方面,Tinyxml需要调用者从根节点开始查找(使用TiXmlElement类),然后递归找下去,找到到取出属性值。而pugixml使用一个child函数把查找节点这一步都封装好了。比如下面这样一个xml文件:
- <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
- <Profile FormatVersion="1">
- <Tools>
- <Tool Filename="jam" AllowIntercept="true">
- <Description>Jamplus build system</Description>
- </Tool>
- <Tool Filename="mayabatch.exe" AllowRemote="true" OutputFileMasks="*.dae" DeriveCaptionFrom="lastparam" Timeout="40" />
- <Tool Filename="meshbuilder_*.exe" AllowRemote="false" OutputFileMasks="*.mesh" DeriveCaptionFrom="lastparam" Timeout="10" />
- <Tool Filename="texbuilder_*.exe" AllowRemote="true" OutputFileMasks="*.tex" DeriveCaptionFrom="lastparam" />
- <Tool Filename="shaderbuilder_*.exe" AllowRemote="true" DeriveCaptionFrom="lastparam" />
- </Tools>
- </Profile>
使用pugixml简单几句代码就能将所有Tool节点的属性值都输出来:
- pugi::xml_document doc;
- if (!doc.load_file("xgconsole.xml")) return -1;
-
- pugi::xml_node tools = doc.child("Profile").child("Tools");
-
-
- for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling())
- {
- std::cout << "Tool:";
-
- for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute())
- {
- std::cout << " " << attr.name() << "=" << attr.value();
- }
-
- std::cout << std::endl;
- }
-
是不是很方便呢?
pugixml的官方主页为:http://pugixml.org/
继续学习pugixml库使用。xml文件的节点一般有两种方式:
<paramname="version" type="float" value="1.1" />和
<Welcome>Welcometo MyApp</Welcome>
今天我们学习如何使用pugixml库给一个xml文件添加这两种节点。
xml的原文件是:
- <?xml version="1.0"?>
- <MyApp>
- <Messages>
- <Welcome>Welcome to MyApp</Welcome>
- <Farewell>Thank you for using MyApp</Farewell>
- </Messages>
- <Windows>
- <Window name="MainFrame" x="5" y="15" w="400" h="250" />
- </Windows>
- </MyApp>
我们要达到的效果是:
- <?xml version="1.0"?>
- <MyApp>
- <Messages>
- <Welcome>Welcome to MyApp</Welcome>
- <Farewell>Thank you for using MyApp</Farewell>
- </Messages>
- <Windows>
- <Window name="MainFrame" x="5" y="15" w="400" h="250" />
- <param name="version" type="float" value="1.1" />
- <Welcome>Welcome to MyApp</Welcome>
- <param name="version" type="float" value="1.1" />
- <Farewell>Thank you for using MyApp</Farewell>
- </Windows>
- </MyApp>
实现代码如下,代码较为简单,这里就不作详细解释了:
- #include <vector>
- #include <string>
- #include <pugixml.hpp>
-
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- pugi::xml_document doc;
-
- pugi::xml_parse_result ret = doc.load_file(_T("E:\\ConfigDemo.xml"));
-
- pugi::xml_node Messages = doc.child(_T("MyApp")).child(_T("Messages"));
- std::vector<std::pair<std::string,std::string> > vecNode;
-
-
- for (pugi::xml_node Plugin = Messages.first_child(); Plugin; Plugin = Plugin.next_sibling())
- {
- typedef std::pair<std::string,std::string> string_Pair;
- std::string strNodeName = Plugin.name();
- std::string strValue = Messages.child_value(strNodeName.c_str());
- vecNode.push_back(string_Pair(strNodeName,strValue));
- }
-
- pugi::xml_node window = doc.child(_T("MyApp")).child(_T("Windows"));
- for (int i =0;i<vecNode.size();i++)
- {
-
- pugi::xml_node new_node = window.append_child(vecNode[i].first.c_str());
-
- new_node.append_child(pugi::node_pcdata).set_value(vecNode[i].second.c_str());
-
- pugi::xml_node param = window.insert_child_before("param",new_node);
- param.append_attribute("name") = "version";
- param.append_attribute("value") = 1.1;
- param.insert_attribute_after("type", param.attribute("name")) = "float";
- }
-
- doc.save_file(_T("E:\\ConfigDemo.xml"));
- getchar();
- return 0;
- }