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

2023年10月11日,周三下午


目录


RapidXML的官网

https://rapidxml.sourceforge.net/

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


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

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

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?> <!-- XML声明,指定版本和编码 --> 
<root> <!-- 根元素 --> 
    <Connector connectionTimeout="20000" maxParameterCount="1000" port="8088" protocol="HTTP/1.1" redirectPort="8443"/><!--子元素Connector-->
    <book> <!-- 子元素book -->
        <name>C++ Primer</name>
        <author>Stanley B. Lippman</author>
        <price>59.00</price>
    </book> 
    <book>
        <name>Head First Java</name>
        <author>Kathy Sierra</author>
        <price>35.99</price>
    </book> 
</root>

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

cpp 复制代码
#include "rapidxml.hpp"
#include <iostream>
#include <fstream>

int main() {
    rapidxml::xml_document<> doc;

    // 打开XML文件
    std::ifstream file("ReadExample.xml");
    if (!file) {
        std::cerr << "Failed to open the XML file." << std::endl;
        return 1;
    }

    // 读取XML文件内容到内存
    std::string xml_contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
    file.close();

    // 解析XML文档
    doc.parse<rapidxml::parse_default>(&xml_contents[0]);

    // 获取根元素
    rapidxml::xml_node<>* root = doc.first_node("root");

    // 遍历子元素book
    for (rapidxml::xml_node<>* book = root->first_node("book"); book; book = book->next_sibling("book")) {
        // 获取书名
        rapidxml::xml_node<>* name = book->first_node("name");
        if (name) {
            std::cout << "Book Name: " << name->value() << std::endl;
        }

        // 获取作者
        rapidxml::xml_node<>* author = book->first_node("author");
        if (author) {
            std::cout << "Author: " << author->value() << std::endl;
        }

        // 获取价格
        rapidxml::xml_node<>* price = book->first_node("price");
        if (price) {
            std::cout << "Price: " << price->value() << std::endl;
        }

        std::cout << std::endl;
    }

    // 获取Connector元素的属性
    rapidxml::xml_node<>* connector = root->first_node("Connector");
    if (connector) {
        std::cout << "Connector Attributes:" << std::endl;
        for (rapidxml::xml_attribute<>* attr = connector->first_attribute(); attr; attr = attr->next_attribute()) {
            std::cout << "Attribute Name: " << attr->name() << ", Value: " << attr->value() << std::endl;
        }
    }

    return 0;
}

运行结果

使用rapidXML创建XML文件

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

cpp 复制代码
#include "rapidxml.hpp"
#include "rapidxml_print.hpp" // 用于格式化输出XML
#include <iostream>
#include <fstream>

int main() {
    rapidxml::xml_document<> doc;

    // 创建根元素
    rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");
    doc.append_node(root);

    // 创建一个元素book
    rapidxml::xml_node<>* book = doc.allocate_node(rapidxml::node_element, "book");
    root->append_node(book);

    // 创建book元素的子元素
    rapidxml::xml_node<>* name = doc.allocate_node(rapidxml::node_element, "name", "C++ Primer");
    book->append_node(name);

    rapidxml::xml_node<>* author = doc.allocate_node(rapidxml::node_element, "author", "Stanley B. Lippman");
    book->append_node(author);

    rapidxml::xml_node<>* price = doc.allocate_node(rapidxml::node_element, "price", "59.00");
    book->append_node(price);

    // 创建第二个book元素
    book = doc.allocate_node(rapidxml::node_element, "book");
    root->append_node(book);

    name = doc.allocate_node(rapidxml::node_element, "name", "Head First Java");
    book->append_node(name);

    author = doc.allocate_node(rapidxml::node_element, "author", "Kathy Sierra");
    book->append_node(author);

    price = doc.allocate_node(rapidxml::node_element, "price", "35.99");
    book->append_node(price);

    // 输出XML到文件
    std::ofstream file("created.xml");
    file << doc;
    file.close();

    return 0;
}

如果上面的代码无法运行

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

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

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

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

rapidxml_ext.hpp文件的代码如下

cpp 复制代码
//rapidxml_ext.hpp
#pragma once

#include "rapidxml.hpp"

// Adding declarations to make it compatible with gcc 4.7 and greater
// See https://stackoverflow.com/a/55408678
namespace rapidxml {
    namespace internal {
        template <class OutIt, class Ch>
        inline OutIt print_children(OutIt out, const xml_node<Ch>* node, int flags, int indent);

        template <class OutIt, class Ch>
        inline OutIt print_attributes(OutIt out, const xml_node<Ch>* node, int flags);

        template <class OutIt, class Ch>
        inline OutIt print_data_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

        template <class OutIt, class Ch>
        inline OutIt print_cdata_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

        template <class OutIt, class Ch>
        inline OutIt print_element_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

        template <class OutIt, class Ch>
        inline OutIt print_declaration_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

        template <class OutIt, class Ch>
        inline OutIt print_comment_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

        template <class OutIt, class Ch>
        inline OutIt print_doctype_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

        template <class OutIt, class Ch>
        inline OutIt print_pi_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
    }
}

#include "rapidxml_print.hpp"

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

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

改正的代码后如下

cpp 复制代码
#include "rapidxml.hpp"
#include "rapidxml_ext.hpp"  //只多了这一行
#include "rapidxml_print.hpp" // 用于格式化输出XML
#include <iostream>
#include <fstream>

int main() {
    rapidxml::xml_document<> doc;

    // 创建根元素
    rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");
    doc.append_node(root);

    // 创建一个元素book
    rapidxml::xml_node<>* book = doc.allocate_node(rapidxml::node_element, "book");
    root->append_node(book);

    // 创建book元素的子元素
    rapidxml::xml_node<>* name = doc.allocate_node(rapidxml::node_element, "name", "C++ Primer");
    book->append_node(name);

    rapidxml::xml_node<>* author = doc.allocate_node(rapidxml::node_element, "author", "Stanley B. Lippman");
    book->append_node(author);

    rapidxml::xml_node<>* price = doc.allocate_node(rapidxml::node_element, "price", "59.00");
    book->append_node(price);

    // 创建第二个book元素
    book = doc.allocate_node(rapidxml::node_element, "book");
    root->append_node(book);

    name = doc.allocate_node(rapidxml::node_element, "name", "Head First Java");
    book->append_node(name);

    author = doc.allocate_node(rapidxml::node_element, "author", "Kathy Sierra");
    book->append_node(author);

    price = doc.allocate_node(rapidxml::node_element, "price", "35.99");
    book->append_node(price);

    // 输出XML到文件
    std::ofstream file("created.xml");
    file << doc;
    file.close();

    return 0;
}

运行结果

相关推荐
找藉口是失败者的习惯6 小时前
从传统到未来:Android XML布局 与 Jetpack Compose的全面对比
android·xml
一二小选手6 小时前
【MyBatis】全局配置文件—mybatis.xml 创建xml模板
xml·java·mybatis
乌夷4 天前
在pom.xml中通过repositories在Maven构建过程中访问setting.xml之外的仓库
xml·java·maven
振宇i4 天前
Mybatis xml动态SQL 判断失效问题
xml·sql·mybatis
程序员老王wd6 天前
java xml 文本解析
xml·java
小百菜7 天前
dom4j解析含有命名空间的XML
xml·dom4j
黎明晓月7 天前
MyBatis XML一个方法执行插入或更新操做(PostgreSQL)
xml·postgresql·mybatis
GoKu~7 天前
项目配置文件选择(Json,xml,Yaml, INI)
xml·json
枫叶落雨2228 天前
mybatis-plus: mapper-locations: “classpath*:/mapper/**/*.xml“配置!!!解释
xml·java·mybatis
double丶flower8 天前
mybatis在mapper.xml中怎么处理大于、小于、不等于号
xml·java·mybatis