加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

TinyXml 读写XML文件 .

发布时间:2020-12-16 05:28:57 所属栏目:百科 来源:网络整理
导读:使用Tinyxml创建和读取XML文件的优点:1,可在Windows和Linux中使用;2,方便易学,易用,可在 http://sourceforge.net/projects/tinyxml/ 获取源代码。将其中的文件tinystr.h,tinyxml.h,tinystr.cpp,tinyxmlerror.cpp,tinyxmlparser.cpp和tinyxml.cpp拷
使用Tinyxml创建和读取XML文件的优点:1,可在Windows和Linux中使用;2,方便易学,易用,可在 http://sourceforge.net/projects/tinyxml/ 获取源代码。将其中的文件tinystr.h,tinyxml.h,tinystr.cpp,tinyxmlerror.cpp,tinyxmlparser.cpp和tinyxml.cpp拷贝到您的工程目录,即可方便使用。

下边我写了一个简单的测试程序,创建XML文件,之后从该文件中读取XML节点元素。测试程序在VS2005中测试通过。

[cpp] view plain copy print ?
  1. #include<iostream>
  2. #include"tinyxml.h"
  3. usingnamespacestd;
  4. #pragmawarning(disable:4996)
  5. #defineXML_STR_FILE"cat.xml"
  6. #defineXML_STR_ROOT"xml-root"
  7. #defineXML_STR_CAT_INFO"cat-info"
  8. #defineXML_STR_CAT_NUM"num"
  9. #defineXML_STR_CAT_AGE"age"
  10. #defineXML_STR_CAT_COLOR"color"
  11. #defineXML_STR_CAT_NAME"name"
  12. #defineIN
  13. #defineOUT
  14. #defineRET_OK0
  15. #defineRET_ERR-1
  16. #defineNAME_LENGTH20
  17. #defineCOLOR_LENGTH20
  18. #defineBUF_SIZE32
  19. #defineXML_RETURN(x){if(RET_OK!=(x))returnRET_ERR;}
  20. #defineXML_ASSERT(x){if(NULL==(x))returnRET_ERR;}
  21. structCAT_INFO
  22. {
  23. CAT_INFO():iAge(0)
  24. {
  25. szName[0]='/0';
  26. szColor[0]='/0';
  27. };
  28. intiAge;//age
  29. charszName[NAME_LENGTH];//name
  30. charszColor[COLOR_LENGTH];//color
  31. };
  32. typedefCAT_INFO*PCAT_INFO;
  33. classCat_List_Info
  34. {
  35. public:
  36. Cat_List_Info(boolbDel):bIsDelete(bDel),iNum(0),pCatList(NULL)
  37. {
  38. };
  39. ~Cat_List_Info()
  40. {
  41. if(bIsDelete&&pCatList)
  42. {
  43. delete[]pCatList;
  44. pCatList=NULL;
  45. }
  46. };
  47. public:
  48. intiNum;//catsnumber
  49. PCAT_INFOpCatList;//list
  50. private:
  51. boolbIsDelete;
  52. };
  53. //addleafnode
  54. intAddLeafNode(TiXmlNode*pElmParent,constchar*pszNode,constchar*pszText)
  55. {
  56. TiXmlElementelmNode(pszNode);
  57. TiXmlTextelmText(pszText);
  58. XML_ASSERT(elmNode.InsertEndChild(elmText));
  59. XML_ASSERT(pElmParent->InsertEndChild(elmNode));
  60. returnRET_OK;
  61. }
  62. //getleafnode
  63. intGetLeafNode(TiXmlNode*pElmParent,char*pszNode,char*pszText)
  64. {
  65. TiXmlNode*pTemp;
  66. if(pElmParent&&(pTemp=pElmParent->FirstChild(pszNode)))
  67. {
  68. if(pTemp=pTemp->FirstChild())
  69. {
  70. strcpy(pszText,pTemp->Value());
  71. returnRET_OK;
  72. }
  73. }
  74. returnRET_ERR;
  75. }
  76. intWriteXmlToFile(INCat_List_Info*pCats)
  77. {
  78. TiXmlDeclarationDeclaration("1.0","","");
  79. TiXmlDocumentxmlDoc(XML_STR_FILE);
  80. xmlDoc.InsertEndChild(Declaration);
  81. TiXmlElementelmRoot(XML_STR_ROOT);
  82. charszBuf[BUF_SIZE];
  83. szBuf[0]='/0';
  84. if(!pCats)
  85. {
  86. returnRET_ERR;
  87. }
  88. if(pCats->iNum>0)
  89. {
  90. //num
  91. sprintf(szBuf,"%d",pCats->iNum);
  92. XML_RETURN(AddLeafNode(&elmRoot,XML_STR_CAT_NUM,szBuf));
  93. }
  94. else
  95. {
  96. returnRET_ERR;
  97. }
  98. for(inti=0;i<pCats->iNum;i++)
  99. {
  100. TiXmlElementelmCat(XML_STR_CAT_INFO);
  101. //name
  102. if('/0'!=pCats->pCatList[i].szName[0])
  103. {
  104. XML_RETURN(AddLeafNode(&elmCat,XML_STR_CAT_NAME,pCats->pCatList[i].szName));
  105. }
  106. //age
  107. sprintf(szBuf,pCats->pCatList[i].iAge);
  108. XML_RETURN(AddLeafNode(&elmCat,XML_STR_CAT_AGE,szBuf));
  109. //color
  110. if('/0'!=pCats->pCatList[i].szColor[0])
  111. {
  112. XML_RETURN(AddLeafNode(&elmCat,XML_STR_CAT_COLOR,pCats->pCatList[i].szColor));
  113. }
  114. XML_ASSERT(elmRoot.InsertEndChild(elmCat));
  115. }
  116. XML_ASSERT(xmlDoc.InsertEndChild(elmRoot));
  117. //save
  118. xmlDoc.SaveFile();
  119. returnRET_OK;
  120. }
  121. intReadXmlFromFile(OUTCat_List_Info*pCats)
  122. {
  123. TiXmlElement*pRootNode=NULL;
  124. TiXmlElement*pTemp=NULL;
  125. charszBuf[BUF_SIZE];
  126. szBuf[0]='/0';
  127. if(!pCats)
  128. {
  129. returnRET_ERR;
  130. }
  131. TiXmlDocumentxmlDoc(XML_STR_FILE);
  132. if(!xmlDoc.LoadFile())
  133. returnRET_ERR;
  134. XML_ASSERT(pRootNode=xmlDoc.RootElement());
  135. pTemp=pRootNode->FirstChildElement(XML_STR_CAT_NUM);
  136. if(pTemp)
  137. {
  138. GetLeafNode(pRootNode,szBuf);
  139. pCats->iNum=atoi(szBuf);
  140. }
  141. if(pCats->iNum>0)
  142. {
  143. TiXmlNode*pCat=NULL;
  144. pCat=pRootNode->FirstChild(XML_STR_CAT_INFO);
  145. if(pCat)
  146. {
  147. for(inti=0;i<pCats->iNum;i++)
  148. {
  149. //name
  150. GetLeafNode(pCat,pCats->pCatList[i].szName);
  151. //age
  152. GetLeafNode(pCat,szBuf);
  153. pCats->pCatList[i].iAge=atoi(szBuf);
  154. //color
  155. GetLeafNode(pCat,pCats->pCatList[i].szColor);
  156. pCat=pCat->NextSibling();
  157. }
  158. }
  159. }
  160. else
  161. {
  162. returnRET_ERR;
  163. }
  164. returnRET_OK;
  165. }
  166. intmain(intargc,char*argv[])
  167. {
  168. Cat_List_Infocats(true);
  169. cats.iNum=2;
  170. cats.pCatList=newCAT_INFO[cats.iNum];
  171. if(NULL==cats.pCatList)
  172. {
  173. cout<<"nomemory..."<<endl;
  174. returnRET_ERR;
  175. }
  176. cats.pCatList[0].iAge=5;
  177. strcpy(cats.pCatList[0].szColor,"white");
  178. strcpy(cats.pCatList[0].szName,"Bob");
  179. cats.pCatList[1].iAge=6;
  180. strcpy(cats.pCatList[1].szColor,"black");
  181. strcpy(cats.pCatList[1].szName,"Tom");
  182. intiRet=WriteXmlToFile(&cats);
  183. iRet=ReadXmlFromFile(&cats);
  184. if(RET_OK==iRet)
  185. {
  186. for(inti=0;i<cats.iNum;i++)
  187. {
  188. cout<<i<<"cat'snameis"<<cats.pCatList[i].szName<<endl;
  189. cout<<i<<"cat'sageis"<<cats.pCatList[i].iAge<<endl;
  190. cout<<i<<"cat'scoloris"<<cats.pCatList[i].szColor<<endl;
  191. }
  192. }
  193. system("pause");
  194. return0;
  195. }

下边是生成的cat.xml文件

[xhtml] view plain copy print ?
  1. <?xmlversion="1.0"?>
  2. <xml-root>
  3. <num>2</num>
  4. <cat-info>
  5. <name>Bob</name>
  6. <age>5</age>
  7. <color>white</color>
  8. </cat-info>
  9. <cat-info>
  10. <name>Tom</name>
  11. <age>6</age>
  12. <color>black</color>
  13. </cat-info>
  14. </xml-root>

从cat.xml文件读取节点元素,进行打印

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读