用tinyxml 库创建并读写xml代码截取

前端之家收集整理的这篇文章主要介绍了用tinyxml 库创建并读写xml代码截取前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 因为自己每次写完之后都忘记了 然后又要从别的地方学习再重新写 还不如记录再这里
  2.  
  3. //创建
  4. TiXmlDocument *pXmlDocument = new TiXmlDocument(m_strFilePath.c_str());//
  5. TiXmlDeclaration *pDeclaretion = new TiXmlDeclaration("1.0","UTF-8","");//创建xml声明
  6. pXmlDocument->LinkEndChild(pDeclaretion);
  7. TiXmlElement *pXmlElement = new TiXmlElement("Symbol");
  8. pXmlDocument->LinkEndChild(pXmlElement);
  9. pXmlDocument->SaveFile(m_strFilePath.c_str());
  10.  
  11. //读
  12. TiXmlDocument *pXmlDoc = new TiXmlDocument();
  13. pXmlDoc->LoadFile(strFilePath.c_str());
  14.  
  15. TiXmlElement *pRootElement = pXmlDoc->RootElement();
  16. if (pRootElement == NULL)
  17. {
  18. return;
  19. }
  20. // 第一个子节点
  21. TiXmlElement *pSignNode = pRootElement->FirstChildElement("sign");
  22. if (!pSignNode)
  23. {
  24. SAFE_DELETE(pSignNode);
  25. return;
  26. }
  27. while (pSignNode != NULL)
  28. {
  29. CMapSingleParameter *pMapSingleParameter = new CMapSingleParameter();
  30. if (!pMapSingleParameter)
  31. {
  32. SAFE_DELETE(pMapSingleParameter);
  33. return ;
  34. }
  35. // text
  36. TCHAR tcharValue[MAX_PATH * 4] = {_T('\0')};
  37.  
  38. // name
  39. const CHAR *pszName = pSignNode->Attribute("name");
  40. if (pszName != NULL)
  41. {
  42. CommonUtil::UTF8ToTCHAR(pszName,tcharValue);
  43. pMapSingleParameter->AppendParameter(_T("name"),tcharValue);
  44. }
  45. // height
  46. const CHAR *pszHeight = pSignNode->Attribute("height");
  47. if (pszHeight != NULL)
  48. {
  49. CommonUtil::UTF8ToTCHAR(pszHeight,tcharValue);
  50. pMapSingleParameter->AppendParameter(_T("height"),tcharValue);
  51. }
  52. //Content
  53. const CHAR *pszContent = pSignNode->GetText();
  54. if (pszContent != NULL)
  55. {
  56. CommonUtil::UTF8ToTCHAR(pszContent,tcharValue);
  57. pMapSingleParameter->AppendParameter(_T("content"),tcharValue);
  58. }
  59.  
  60. m_pLatelyVectorSymbol->push_back(pMapSingleParameter);
  61. pSignNode = pSignNode->NextSiblingElement();
  62. }
  63.  
  64.  
  65.  
  66. //写
  67. string strFile = m_strFilePath;
  68. //创建文档对象
  69. TiXmlDocument myXmlDocument;
  70. //加载文件数据
  71. myXmlDocument.LoadFile(strFile.c_str());
  72.  
  73. TiXmlElement *pRootElement = myXmlDocument.RootElement();
  74. if (pRootElement == NULL)
  75. {
  76. return ;
  77. }
  78.  
  79. if (pRootElement != NULL)
  80. {
  81. TiXmlElement *pFirstNode = pRootElement->FirstChildElement("sign");
  82. if (pFirstNode == NULL)
  83. {
  84. int nSignPos = 0;
  85.  
  86. for (TVectorSymbol::iterator ite = m_pLatelyVectorSymbol->begin(); ite != m_pLatelyVectorSymbol->end(); ++ite)
  87. {
  88. CMapSingleParameter *pMapSingleParameter = *ite;
  89. TiXmlElement *insertElement = new TiXmlElement("sign");
  90. pRootElement->LinkEndChild(insertElement);
  91. xstring strUnicode = pMapSingleParameter->GetParameterValue(_T("name"));
  92. TCHAR tcharValue[MAX_PATH*4]= {_T('\0')};
  93. _tcscpy(tcharValue,strUnicode.c_str());
  94. char chUtf8[MAX_PATH*4] = {'\0'};
  95. CommonUtil::TCHARToUTF8(tcharValue,chUtf8);
  96. insertElement->SetAttribute("name",chUtf8);
  97.  
  98.  
  99. strUnicode = pMapSingleParameter->GetParameterValue(_T("height"));
  100. _tcscpy(tcharValue,strUnicode.c_str());
  101. CommonUtil::TCHARToUTF8(tcharValue,chUtf8);
  102. insertElement->SetAttribute("height",chUtf8);
  103.  
  104. strUnicode = pMapSingleParameter->GetParameterValue(_T("content"));
  105. _tcscpy(tcharValue,chUtf8);
  106. TiXmlText *pXmlText = new TiXmlText(chUtf8);
  107. pXmlText->SetCDATA(true);//添加格式化<![CDATA[]]>
  108. insertElement->LinkEndChild(pXmlText);
  109.  
  110. m_nLatelyTotal ++;
  111. }
  112. }
  113. }
  114. myXmlDocument.SaveFile(strFile.c_str());
  115. }
  116.  
  117.  
  118. //删除节点
  119.  
  120. string strFile = m_strFilePath;
  121.  
  122. //创建文档对象
  123. TiXmlDocument myXmlDocument;
  124. //加载文件数据
  125. myXmlDocument.LoadFile(strFile.c_str());
  126.  
  127. TiXmlElement *pRootElement = myXmlDocument.RootElement();
  128. if (pRootElement == NULL)
  129. {
  130. return ;
  131. }
  132.  
  133. TiXmlElement *pSignNode = pRootElement->FirstChildElement("sign");
  134. while (pSignNode != NULL)
  135. {
  136. m_nLatelyTotal --;
  137. TiXmlElement *pSignRemoveNode = pSignNode;
  138. pSignNode= pSignNode->NextSiblingElement("sign");
  139. pRootElement->RemoveChild(pSignRemoveNode);
  140. }
  141.  
  142. myXmlDocument.SaveFile(strFile.c_str());

猜你在找的XML相关文章