解析例子

前端之家收集整理的这篇文章主要介绍了解析例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. // xml_test1.cpp : 定义控制台应用程序的入口点。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <vector>
  7. #include <string>
  8. #include "pugixml.hpp"
  9.  
  10. #pragma comment(lib,"pugixmld.lib")
  11. int _tmain(int argc,_TCHAR* argv[])
  12. {
  13. pugi::xml_document doc;
  14. // 加载xml文件
  15. pugi::xml_parse_result ret = doc.load_file(_T("test.xml"));
  16.  
  17. pugi::xml_node Messages = doc.child("MyApp").child("Messages");
  18. std::vector<std::pair<std::string,std::string> > vecNode;
  19.  
  20. // 读取原有的Welcome 节点和Farewell节点,并保存其值
  21. for (pugi::xml_node Plugin = Messages.first_child(); Plugin; Plugin = Plugin.next_sibling())
  22. {
  23. typedef std::pair<std::string,std::string> string_Pair;
  24. std::string strNodeName = Plugin.name();
  25. std::string strValue = Messages.child_value(strNodeName.c_str());
  26. vecNode.push_back(string_Pair(strNodeName,strValue));
  27. }
  28.  
  29. pugi::xml_node window = doc.child("MyApp").child("Windows");
  30. for (int i =0;i<vecNode.size();i++)
  31. {
  32. // 添加Welcome 节点和Farewell节点
  33. pugi::xml_node new_node = window.append_child(vecNode[i].first.c_str());
  34.  
  35. new_node.append_child(pugi::node_pcdata).set_value(vecNode[i].second.c_str());
  36. // 添加param 节点
  37. pugi::xml_node param = window.insert_child_before("param",new_node);
  38. param.append_attribute("name") = "version";
  39. param.append_attribute("value") = 1.1;
  40. param.insert_attribute_after("type",param.attribute("name")) = "float";
  41. }
  42.  
  43. doc.save_file(_T("test.xml"));
  44. getchar();
  45. return 0;
  46. }



xml 文件

  1. <?xml version="1.0"?>
  2. <MyApp>
  3. <Messages>
  4. <Welcome>Welcome to MyApp</Welcome>
  5. <Farewell>Thank you for using MyApp</Farewell>
  6. </Messages>
  7. <Windows>
  8. <Window name="MainFrame" x="5" y="15" w="400" h="250" />
  9. <param name="version" type="float" value="1.1" />
  10. <Welcome>Welcome to MyApp</Welcome>
  11. <param name="version" type="float" value="1.1" />
  12. <Farewell>Thank you for using MyApp</Farewell>
  13. </Windows>
  14. </MyApp>

猜你在找的XML相关文章