您当前的位置: 首页 > 

TinyXml快速入门(一)

发布时间:2012-06-04 11:52:00 ,浏览量:0

作者:朱金灿 来源:http://blog.csdn.net/clever101

对于xml文件,目前我的工作只是集中在配置文件和作为简单的信息文件来用,因此我不太喜欢使用msxml这种重量级的xml解析器,特别是使用msxml解析xml涉及到复杂的com类型转换,更是令人感觉繁琐。因此对于简单的xml文件的解析,我更愿意使用开源的TinyXml。

首先介绍一下TinyXml吧。TinyXML是目前非常流行的一款基于DOM模型的XML解析器,简单易用且小巧玲珑,非常适合存储简单数据,配置文件,对象序列化等数据量不是很大的操作,其主页是:http://www.grinninglizard.com/tinyxml/,目前最新版本是2.5.3 版本。

TinyXml网上的教程很多,但是我觉得写得都不怎样(感觉就是看完之后就没学会)。没办法,只得自己整理一篇适合自己的,至于适不适合别人,就见仁见智了。我感觉xml文件本质就是小型的数据库,换个角度来说就是,你对数据库有什么操作你对xml文件就应能实现什么操作。一般而言,对数据库的操作包括以下几种:新建数据库、查询数据库、修改数据库和删除数据库。那么对应xml文件就是新建xml文件、查询xml文件的指定节点的值,修改xml文件中节点的值和删除xml文件中节点的值。

首先我们认识一下xml文件有哪几种形式。下面我列出一些常用的xml文件的形式:

[xhtml] view plain copy
  1. example1.xml:
  2. //定义一个TiXmlDocument类指针
  3. TiXmlDocument*pDoc=newTiXmlDocument;
  4. if(NULL==pDoc)
  5. {
  6. returnfalse;
  7. }
  8. TiXmlDeclaration*pDeclaration=newTiXmlDeclaration(_T("1.0"),_T(""),_T(""));
  9. if(NULL==pDeclaration)
  10. {
  11. returnfalse;
  12. }
  13. pDoc->LinkEndChild(pDeclaration);
  14. //生成一个根节点:MyApp
  15. TiXmlElement*pRootEle=newTiXmlElement(_T("MyApp"));
  16. if(NULL==pRootEle)
  17. {
  18. returnfalse;
  19. }
  20. pDoc->LinkEndChild(pRootEle);
  21. //生成子节点:Messages
  22. TiXmlElement*pMsg=newTiXmlElement(_T("Messages"));
  23. if(NULL==pMsg)
  24. {
  25. returnfalse;
  26. }
  27. pRootEle->LinkEndChild(pMsg);
  28. //生成子节点:Welcome
  29. TiXmlElement*pWelcome=newTiXmlElement(_T("Welcome"));
  30. if(NULL==pWelcome)
  31. {
  32. returnfalse;
  33. }
  34. pMsg->LinkEndChild(pWelcome);
  35. //设置Welcome节点的值
  36. std::stringstrValue=_T("WelcometoMyApp");
  37. TiXmlText*pWelcomeValue=newTiXmlText(strValue);
  38. pWelcome->LinkEndChild(pWelcomeValue);
  39. //生成子节点:Farewell
  40. TiXmlElement*pFarewell=newTiXmlElement(_T("Farewell"));
  41. if(NULL==pFarewell)
  42. {
  43. returnfalse;
  44. }
  45. pMsg->LinkEndChild(pFarewell);
  46. //设置Farewell节点的值
  47. strValue=_T("ThankyouforusingMyApp");
  48. TiXmlText*pFarewellValue=newTiXmlText(strValue);
  49. pFarewell->LinkEndChild(pFarewellValue);
  50. //生成子节点:Windows
  51. TiXmlElement*pWindows=newTiXmlElement(_T("Windows"));
  52. if(NULL==pWindows)
  53. {
  54. returnfalse;
  55. }
  56. pRootEle->LinkEndChild(pWindows);
  57. //生成子节点:Window
  58. TiXmlElement*pWindow=newTiXmlElement(_T("Window"));
  59. if(NULL==pWindow)
  60. {
  61. returnfalse;
  62. }
  63. pWindows->LinkEndChild(pWindow);
  64. //设置节点Window的值
  65. pWindow->SetAttribute(_T("name"),_T("MainFrame"));
  66. pWindow->SetAttribute(_T("x"),_T("5"));
  67. pWindow->SetAttribute(_T("y"),_T("15"));
  68. pWindow->SetAttribute(_T("w"),_T("400"));
  69. pWindow->SetAttribute(_T("h"),_T("250"));
  70. //生成子节点:Window
  71. TiXmlElement*pConnection=newTiXmlElement(_T("Connection"));
  72. if(NULL==pConnection)
  73. {
  74. returnfalse;
  75. }
  76. pRootEle->LinkEndChild(pConnection);
  77. //设置节点Connection的值
  78. pConnection->SetAttribute(_T("ip"),_T("192.168.0.1"));
  79. pConnection->SetAttribute(_T("timeout"),_T("123.456000"));
  80. pDoc->SaveFile(XmlFile);
  81. returntrue;
  82. }

不知你注意到上面的规律没有?首先父节点连接字节点使用函数LinkEndChild,使用方法是:pParentNode-> LinkEndChild(pChild);其次设置类似这种结构采用SetAttribute函数,这个函数有两个参数,前一个参数表示键,后一个参数表示键值,设置Thank you for using MyApp这种结构采用TiXmlText类,使用LinkEndChild函数进行连结。

上面是创建xml文件的代码,下面介绍读取xml文件的代码。打印整个xml文件的代码很简单,代码如下:

[cpp] view plain copy
  1. /*!
  2. */brief打印xml文件。
  3. *
  4. */paramXmlFilexml文件全路径。
  5. */return是否成功。true为成功,false表示失败。
  6. */
  7. boolPaintXml(std::stringXmlFile)
  8. {
  9. //定义一个TiXmlDocument类指针
  10. TiXmlDocument*pDoc=newTiXmlDocument();
  11. if(NULL==pDoc)
  12. {
  13. returnfalse;
  14. }
  15. pDoc->LoadFile(XmlFile);
  16. pDoc->Print();
  17. returntrue;
  18. }

下次介绍使用tinyxml库对xml文件进行查询指定节点、删除指定节点、修改指定节点和增加节点的用法。

参考文献:

1.《TinyXML入门教程》

2. 《tinyxml 使用笔记与总结》

3. 《TinyXML Tutorial 中文指南》

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    109966博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.2816s