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

TinyXml 读写XML文件

发布时间:2020-12-16 09:05: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
  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. szName[0]='/0';
  25. szColor[0]='/0';
  26. };
  27. intiAge;//age
  28. charszName[NAME_LENGTH];//name
  29. charszColor[COLOR_LENGTH];//color
  30. typedefCAT_INFO*PCAT_INFO;
  31. classCat_List_Info
  32. public:
  33. Cat_List_Info(boolbDel):bIsDelete(bDel),iNum(0),pCatList(NULL)
  34. {
  35. ~Cat_List_Info()
  36. if(bIsDelete&&pCatList)
  37. delete[]pCatList;
  38. pCatList=NULL;
  39. }
  40. public:
  41. intiNum;//catsnumber
  42. PCAT_INFOpCatList;//list
  43. private:
  44. boolbIsDelete;
  45. //addleafnode
  46. intAddLeafNode(TiXmlNode*pElmParent,constchar*pszNode,87); background-color:inherit; font-weight:bold">char*pszText)
  47. TiXmlElementelmNode(pszNode);
  48. TiXmlTextelmText(pszText);
  49. XML_ASSERT(elmNode.InsertEndChild(elmText));
  50. XML_ASSERT(pElmParent->InsertEndChild(elmNode));
  51. returnRET_OK;
  52. //getleafnode
  53. intGetLeafNode(TiXmlNode*pElmParent,char*pszText)
  54. TiXmlNode*pTemp;
  55. if(pElmParent&&(pTemp=pElmParent->FirstChild(pszNode)))
  56. if(pTemp=pTemp->FirstChild())
  57. strcpy(pszText,pTemp->Value());
  58. returnRET_OK;
  59. }
  60. returnRET_ERR;
  61. intWriteXmlToFile(INCat_List_Info*pCats)
  62. TiXmlDeclarationDeclaration("1.0","","");
  63. TiXmlDocumentxmlDoc(XML_STR_FILE);
  64. xmlDoc.InsertEndChild(Declaration);
  65. TiXmlElementelmRoot(XML_STR_ROOT);
  66. charszBuf[BUF_SIZE];
  67. szBuf[0]='/0';
  68. if(!pCats)
  69. if(pCats->iNum>0)
  70. //num
  71. sprintf(szBuf,"%d",pCats->iNum);
  72. XML_RETURN(AddLeafNode(&elmRoot,XML_STR_CAT_NUM,szBuf));
  73. else
  74. for(inti=0;i<pCats->iNum;i++)
  75. TiXmlElementelmCat(XML_STR_CAT_INFO);
  76. //name
  77. if('/0'!=pCats->pCatList[i].szName[0])
  78. XML_RETURN(AddLeafNode(&elmCat,XML_STR_CAT_NAME,pCats->pCatList[i].szName));
  79. //age
  80. if('/0'!=pCats->pCatList[i].szColor[0])
  81. XML_ASSERT(elmRoot.InsertEndChild(elmCat));
  82. XML_ASSERT(xmlDoc.InsertEndChild(elmRoot));
  83. //save
  84. xmlDoc.SaveFile();
  85. intReadXmlFromFile(OUTCat_List_Info*pCats)
  86. TiXmlElement*pRootNode=NULL;
  87. TiXmlElement*pTemp=NULL;
  88. TiXmlDocumentxmlDoc(XML_STR_FILE);
  89. if(!xmlDoc.LoadFile())
  90. XML_ASSERT(pRootNode=xmlDoc.RootElement());
  91. pTemp=pRootNode->FirstChildElement(XML_STR_CAT_NUM);
  92. if(pTemp)
  93. GetLeafNode(pRootNode,szBuf);
  94. pCats->iNum=atoi(szBuf);
  95. TiXmlNode*pCat=NULL;
  96. pCat=pRootNode->FirstChild(XML_STR_CAT_INFO);
  97. if(pCat)
  98. GetLeafNode(pCat,pCats->pCatList[i].szName);
  99. pCats->pCatList[i].iAge=atoi(szBuf);
  100. GetLeafNode(pCat,pCats->pCatList[i].szColor);
  101. pCat=pCat->NextSibling();
  102. else
  103. returnRET_ERR;
  104. intmain(intargc,87); background-color:inherit; font-weight:bold">char*argv[])
  105. Cat_List_Infocats(true);
  106. cats.iNum=2;
  107. cats.pCatList=newCAT_INFO[cats.iNum];
  108. if(NULL==cats.pCatList)
  109. cout<<"nomemory..."<<endl;
  110. cats.pCatList[0].iAge=5;
  111. strcpy(cats.pCatList[0].szColor,"white");
  112. strcpy(cats.pCatList[0].szName,"Bob");
  113. cats.pCatList[1].iAge=6;
  114. strcpy(cats.pCatList[1].szColor,"black");
  115. strcpy(cats.pCatList[1].szName,"Tom");
  116. intiRet=WriteXmlToFile(&cats);
  117. iRet=ReadXmlFromFile(&cats);
  118. if(RET_OK==iRet)
  119. inti=0;i<cats.iNum;i++)
  120. cout<<i<<"cat'snameis"<<cats.pCatList[i].szName<<endl;
  121. cout<<i<<"cat'sageis"<<cats.pCatList[i].iAge<<endl;
  122. cout<<i<<"cat'scoloris"<<cats.pCatList[i].szColor<<endl;
  123. system("pause");
  124. return0;
  125. }

下边是生成的cat.xml文件

[xhtml] copy
    <?xmlversion="1.0"?>
  1. <xml-root>
  2. num>2</>
  3. cat-infoname>Bobage>5color>white>Tom>6>black>

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

(编辑:李大同)

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

    推荐文章
      热点阅读