• tinyxml2开源库使用


    源码下载:GitHub - leethomason/tinyxml2: TinyXML2 is a simple, small, efficient, C++ XML parser that can be easily integrated into other programs.

    1.加载tinyxml2库

    解压上面现在的压缩包,将tinyxml2.h/tinyxml2.cpp添加到项目工程当中,要使用该库时,只需要使用对于的头文件即可

    #include "tinyxml2.h"

    2.创建XML文件
    1. int test_tinyxml2_create()
    2. {
    3. const char *declaration = "";
    4. tinyxml2::XMLDocument doc;
    5. tinyxml2::XMLError ret = doc.Parse(declaration);
    6. if (ret != 0)
    7. {
    8. fprintf(stderr, "fail to parse xml file: %s\n", declaration);
    9. return -1;
    10. }
    11. tinyxml2::XMLComment *comment = doc.NewComment("this is a xml test file");
    12. doc.InsertEndChild(comment);
    13. tinyxml2::XMLElement *root = doc.NewElement("Root");
    14. doc.InsertEndChild(root);
    15. // User
    16. tinyxml2::XMLElement *user = doc.NewElement("User");
    17. user->SetAttribute("Name", "fengbingchun");
    18. root->InsertEndChild(user);
    19. tinyxml2::XMLElement *blog = doc.NewElement("Blog");
    20. tinyxml2::XMLText *text1 = doc.NewText("CSDN");
    21. blog->InsertEndChild(text1);
    22. user->InsertEndChild(blog);
    23. tinyxml2::XMLElement *code = doc.NewElement("Code");
    24. tinyxml2::XMLText *text2 = doc.NewText("GitHub");
    25. code->InsertEndChild(text2);
    26. user->InsertEndChild(code);
    27. // Blog
    28. tinyxml2::XMLElement *blog2 = doc.NewElement("Blog");
    29. blog2->SetAttribute("Name", "CSDN");
    30. root->InsertEndChild(blog2);
    31. tinyxml2::XMLElement *addr = doc.NewElement("Address");
    32. tinyxml2::XMLText *text3 = doc.NewText("https://blog.csdn.net/fengbingchun");
    33. addr->InsertEndChild(text3);
    34. blog2->InsertEndChild(addr);
    35. tinyxml2::XMLElement *id = doc.NewElement("ID");
    36. tinyxml2::XMLText *text4 = doc.NewText("fengbingchun");
    37. id->InsertEndChild(text4);
    38. blog2->InsertEndChild(id);
    39. // Code
    40. tinyxml2::XMLElement *code2 = doc.NewElement("Code");
    41. code2->SetAttribute("Name", "GitHub");
    42. root->InsertEndChild(code2);
    43. tinyxml2::XMLElement *addr2 = doc.NewElement("Address");
    44. tinyxml2::XMLText *text5 = doc.NewText("https://github.com//fengbingchun");
    45. addr2->InsertEndChild(text5);
    46. code2->InsertEndChild(addr2);
    47. tinyxml2::XMLElement *repositories = doc.NewElement("Repositories");
    48. tinyxml2::XMLText *text6 = doc.NewText("27");
    49. repositories->InsertEndChild(text6);
    50. code2->InsertEndChild(repositories);
    51. #ifdef _MSC_VER
    52. const char *file_name = "E:/GitCode/Messy_Test/testdata/test.xml";
    53. #else
    54. const char *file_name = "testdata/test.xml";
    55. #endif
    56. ret = doc.SaveFile(file_name);
    57. if (ret != 0)
    58. {
    59. fprintf(stderr, "fail to save xml file: %s\n", file_name);
    60. return -1;
    61. }
    62. return 0;
    63. }

    结果:

    1. "1.0" encoding="UTF-8"?>
    2. <Root>
    3. <User Name="fengbingchun">
    4. <Blog>CSDNBlog>
    5. <Code>GitHubCode>
    6. User>
    7. <Blog Name="CSDN">
    8. <Address>https://blog.csdn.net/fengbingchunAddress>
    9. <ID>fengbingchunID>
    10. Blog>
    11. <Code Name="GitHub">
    12. <Address>https://github.com//fengbingchunAddress>
    13. <Repositories>27Repositories>
    14. Code>
    15. Root>
    3.解析XML文件
    1. int test_tinyxml2_parse()
    2. {
    3. #ifdef _MSC_VER
    4. const char *file_name = "E:/GitCode/Messy_Test/testdata/test_tinyxml2.xml";
    5. #else
    6. const char *file_name = "testdata/test_tinyxml2.xml";
    7. #endif
    8. tinyxml2::XMLDocument doc;
    9. tinyxml2::XMLError ret = doc.LoadFile(file_name);
    10. if (ret != 0)
    11. {
    12. fprintf(stderr, "fail to load xml file: %s\n", file_name);
    13. return -1;
    14. }
    15. tinyxml2::XMLElement *root = doc.RootElement();
    16. fprintf(stdout, "root element name: %s\n", root->Name());
    17. // User
    18. tinyxml2::XMLElement *user = root->FirstChildElement("User");
    19. if (!user)
    20. {
    21. fprintf(stderr, "no child element: User\n");
    22. return -1;
    23. }
    24. fprintf(stdout, "user name: %s\n", user->Attribute("Name"));
    25. tinyxml2::XMLElement *blog = user->FirstChildElement("Blog");
    26. if (!blog)
    27. {
    28. fprintf(stderr, "no child element: Blog, in User\n");
    29. return -1;
    30. }
    31. fprintf(stdout, "blog value: %s\n", blog->GetText());
    32. fprintf(stdout, "code value: %s\n\n", user->FirstChildElement("Code")->GetText());
    33. // Blog
    34. tinyxml2::XMLElement *blog2 = root->FirstChildElement("Blog");
    35. if (!blog2)
    36. {
    37. fprintf(stderr, "no child element: Blog\n");
    38. return -1;
    39. }
    40. fprintf(stdout, "blog name: %s\n", blog2->Attribute("Name"));
    41. tinyxml2::XMLElement *addr = blog2->FirstChildElement("Address");
    42. if (!addr)
    43. {
    44. fprintf(stderr, "no child element: Address, in Blog\n");
    45. return -1;
    46. }
    47. fprintf(stdout, "address value: %s\n", addr->GetText());
    48. fprintf(stdout, "id value: %s\n\n", blog2->FirstChildElement("ID")->GetText());
    49. // Code
    50. tinyxml2::XMLElement *code = root->FirstChildElement("Code");
    51. if (!code)
    52. {
    53. fprintf(stderr, "no child element: Code\n");
    54. return -1;
    55. }
    56. fprintf(stdout, "code name: %s\n", code->Attribute("Name"));
    57. tinyxml2::XMLElement *addr2 = code->FirstChildElement("Address");
    58. if (!addr2)
    59. {
    60. fprintf(stderr, "no child element: Address, in Code\n");
    61. return -1;
    62. }
    63. fprintf(stdout, "address value: %s\n", addr2->GetText());
    64. fprintf(stdout, "repositories value: %s\n\n", code->FirstChildElement("Repositories")->GetText());
    65. return 0;
    66. }

  • 相关阅读:
    JavaScript进阶 第二天笔记
    【SQLite快速入门】
    链式法则(Chain Rule)
    IT 安全方案,但是简易版
    遵循这些MySQL设计规范,再也没被组长喷过
    LeetCode 0174. 地下城游戏
    Day29_10 JavaWeb之Servlet域对象、request及response
    网络体系结构
    轮廓匹配---学习笔记
    Bert and its family
  • 原文地址:https://blog.csdn.net/weixin_55238862/article/details/136285383