• 【C++】如何使用RapidXML读取和创建XML文件


    2023年10月11日,周三下午


    目录


    RapidXML的官网

    https://rapidxml.sourceforge.net/

    RapidXML只有头文件,不需要编译和配置。


    使用rapidXML读取XML文件中的元素的属性和值

    此次要读取的XML文件:ReadExample.xml

    1. "1.0" encoding="UTF-8"?>
    2. <root>
    3. <Connector connectionTimeout="20000" maxParameterCount="1000" port="8088" protocol="HTTP/1.1" redirectPort="8443"/>
    4. <book>
    5. <name>C++ Primername>
    6. <author>Stanley B. Lippmanauthor>
    7. <price>59.00price>
    8. book>
    9. <book>
    10. <name>Head First Javaname>
    11. <author>Kathy Sierraauthor>
    12. <price>35.99price>
    13. book>
    14. root>

    用于读取此XML文件的C++代码

    1. #include "rapidxml.hpp"
    2. #include
    3. #include
    4. int main() {
    5. rapidxml::xml_document<> doc;
    6. // 打开XML文件
    7. std::ifstream file("ReadExample.xml");
    8. if (!file) {
    9. std::cerr << "Failed to open the XML file." << std::endl;
    10. return 1;
    11. }
    12. // 读取XML文件内容到内存
    13. std::string xml_contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
    14. file.close();
    15. // 解析XML文档
    16. doc.parse(&xml_contents[0]);
    17. // 获取根元素
    18. rapidxml::xml_node<>* root = doc.first_node("root");
    19. // 遍历子元素book
    20. for (rapidxml::xml_node<>* book = root->first_node("book"); book; book = book->next_sibling("book")) {
    21. // 获取书名
    22. rapidxml::xml_node<>* name = book->first_node("name");
    23. if (name) {
    24. std::cout << "Book Name: " << name->value() << std::endl;
    25. }
    26. // 获取作者
    27. rapidxml::xml_node<>* author = book->first_node("author");
    28. if (author) {
    29. std::cout << "Author: " << author->value() << std::endl;
    30. }
    31. // 获取价格
    32. rapidxml::xml_node<>* price = book->first_node("price");
    33. if (price) {
    34. std::cout << "Price: " << price->value() << std::endl;
    35. }
    36. std::cout << std::endl;
    37. }
    38. // 获取Connector元素的属性
    39. rapidxml::xml_node<>* connector = root->first_node("Connector");
    40. if (connector) {
    41. std::cout << "Connector Attributes:" << std::endl;
    42. for (rapidxml::xml_attribute<>* attr = connector->first_attribute(); attr; attr = attr->next_attribute()) {
    43. std::cout << "Attribute Name: " << attr->name() << ", Value: " << attr->value() << std::endl;
    44. }
    45. }
    46. return 0;
    47. }

    运行结果

    使用rapidXML创建XML文件

    用于创建XML文件的C++代码

    1. #include "rapidxml.hpp"
    2. #include "rapidxml_print.hpp" // 用于格式化输出XML
    3. #include
    4. #include
    5. int main() {
    6. rapidxml::xml_document<> doc;
    7. // 创建根元素
    8. rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");
    9. doc.append_node(root);
    10. // 创建一个元素book
    11. rapidxml::xml_node<>* book = doc.allocate_node(rapidxml::node_element, "book");
    12. root->append_node(book);
    13. // 创建book元素的子元素
    14. rapidxml::xml_node<>* name = doc.allocate_node(rapidxml::node_element, "name", "C++ Primer");
    15. book->append_node(name);
    16. rapidxml::xml_node<>* author = doc.allocate_node(rapidxml::node_element, "author", "Stanley B. Lippman");
    17. book->append_node(author);
    18. rapidxml::xml_node<>* price = doc.allocate_node(rapidxml::node_element, "price", "59.00");
    19. book->append_node(price);
    20. // 创建第二个book元素
    21. book = doc.allocate_node(rapidxml::node_element, "book");
    22. root->append_node(book);
    23. name = doc.allocate_node(rapidxml::node_element, "name", "Head First Java");
    24. book->append_node(name);
    25. author = doc.allocate_node(rapidxml::node_element, "author", "Kathy Sierra");
    26. book->append_node(author);
    27. price = doc.allocate_node(rapidxml::node_element, "price", "35.99");
    28. book->append_node(price);
    29. // 输出XML到文件
    30. std::ofstream file("created.xml");
    31. file << doc;
    32. file.close();
    33. return 0;
    34. }

     如果上面的代码无法运行

    如果你也遇到了如下这样的错误

    那么可以按照下面这两篇文章来创建一个rapidxml_ext.hpp文件

    c++ - RapidXML:无法打印 - 编译时错误 - IT工具网 (coder.work)

    c++ - RapidXML: Unable to print - Compile-time Error - Stack Overflow 

    rapidxml_ext.hpp文件的代码如下 

    1. //rapidxml_ext.hpp
    2. #pragma once
    3. #include "rapidxml.hpp"
    4. // Adding declarations to make it compatible with gcc 4.7 and greater
    5. // See https://stackoverflow.com/a/55408678
    6. namespace rapidxml {
    7. namespace internal {
    8. template <class OutIt, class Ch>
    9. inline OutIt print_children(OutIt out, const xml_node* node, int flags, int indent);
    10. template <class OutIt, class Ch>
    11. inline OutIt print_attributes(OutIt out, const xml_node* node, int flags);
    12. template <class OutIt, class Ch>
    13. inline OutIt print_data_node(OutIt out, const xml_node* node, int flags, int indent);
    14. template <class OutIt, class Ch>
    15. inline OutIt print_cdata_node(OutIt out, const xml_node* node, int flags, int indent);
    16. template <class OutIt, class Ch>
    17. inline OutIt print_element_node(OutIt out, const xml_node* node, int flags, int indent);
    18. template <class OutIt, class Ch>
    19. inline OutIt print_declaration_node(OutIt out, const xml_node* node, int flags, int indent);
    20. template <class OutIt, class Ch>
    21. inline OutIt print_comment_node(OutIt out, const xml_node* node, int flags, int indent);
    22. template <class OutIt, class Ch>
    23. inline OutIt print_doctype_node(OutIt out, const xml_node* node, int flags, int indent);
    24. template <class OutIt, class Ch>
    25. inline OutIt print_pi_node(OutIt out, const xml_node* node, int flags, int indent);
    26. }
    27. }
    28. #include "rapidxml_print.hpp"

     然后在原来的代码的基础上,引入头文件rapidxml_ext.hpp

    注意头文件的顺序,rapidxml_ext.hpp的引入必须先于rapidxml_print.hpp

    改正的代码后如下

    1. #include "rapidxml.hpp"
    2. #include "rapidxml_ext.hpp" //只多了这一行
    3. #include "rapidxml_print.hpp" // 用于格式化输出XML
    4. #include
    5. #include
    6. int main() {
    7. rapidxml::xml_document<> doc;
    8. // 创建根元素
    9. rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");
    10. doc.append_node(root);
    11. // 创建一个元素book
    12. rapidxml::xml_node<>* book = doc.allocate_node(rapidxml::node_element, "book");
    13. root->append_node(book);
    14. // 创建book元素的子元素
    15. rapidxml::xml_node<>* name = doc.allocate_node(rapidxml::node_element, "name", "C++ Primer");
    16. book->append_node(name);
    17. rapidxml::xml_node<>* author = doc.allocate_node(rapidxml::node_element, "author", "Stanley B. Lippman");
    18. book->append_node(author);
    19. rapidxml::xml_node<>* price = doc.allocate_node(rapidxml::node_element, "price", "59.00");
    20. book->append_node(price);
    21. // 创建第二个book元素
    22. book = doc.allocate_node(rapidxml::node_element, "book");
    23. root->append_node(book);
    24. name = doc.allocate_node(rapidxml::node_element, "name", "Head First Java");
    25. book->append_node(name);
    26. author = doc.allocate_node(rapidxml::node_element, "author", "Kathy Sierra");
    27. book->append_node(author);
    28. price = doc.allocate_node(rapidxml::node_element, "price", "35.99");
    29. book->append_node(price);
    30. // 输出XML到文件
    31. std::ofstream file("created.xml");
    32. file << doc;
    33. file.close();
    34. return 0;
    35. }

    运行结果

     

     

  • 相关阅读:
    GoogleTest--事件
    Database之SQL:SQL在线编程、工作中常用SQL代码实践(以语法为导向的增、删、改、查,已基本涵盖所有语法案例)之详细攻略
    电动两轮车智能化浪潮崛起,移远通信以全场景解决方案引领户外出行新变革
    Java整合七牛云对象存储Kodo
    2022年web前端开发学习路线图
    技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
    k8s 基础
    代码随想录Day56 | 1143. 最长公共子序列 | 1035. 不相交的线 | 53. 最大子数组和
    华为云云耀云服务器L实例评测|Elasticsearch的可视化Kibana工具安装 & IK分词器的安装和使用
    什么是接口测试?接口测试的流程步骤
  • 原文地址:https://blog.csdn.net/m0_61629312/article/details/133769004