boost的ptree比qt的接口更适合读写xml,因为它舍弃了结点属性,里面只有根节点和子节点,显得更简洁

就6个接口:

复制代码
写xml文件
boost::property_tree::ptree 动物园;
add<std::string>("名字", "苹果");
add_chile("水果类型",水果);

// 读取 XML 文件
boost::property_tree::read_xml("example.xml", pt);
get_child(),
get<std::string>("SomeValue");或者get_child("Role").data();

					***保存xml***

SaveXML(std::string file_path)

根结点名字:动物园

结点内容: 名字:猫咪 年龄 3 颜色 白

复制代码
创建结点-动物园,内容: 名字:猫咪 年龄 3 颜色 白
创建动物园的字结点,添加水果

boost::property_tree::ptree 动物园;

动物园.add<std::string>("名字", "猫咪");
动物园.add<int>("年龄 ", 3);
动物园.add<std::string>("颜色 ", " 白");

boost::property_tree::ptree 水果;
水果.add<std::string>("名字", "苹果");
动物园.add_chile("水果类型",水果);

保存

复制代码
auto setting = boost::property_tree::xml_writer_make_settings<std::string>('\t', 1, "GB2312");
write_xml(file_path,动物园, std::locale(), setting);

OpenXML(std::string file_path)

复制代码
get_child(),get<std::string>("SomeValue");
get_child(),   get_child("Role").data();
获取结点对象,内容

// 读取 XML 文件
boost::property_tree::read_xml("example.xml", pt);

// 获取根节点的子节点
boost::property_tree::ptree child = pt.get_child("ChildNodeName");

// 从子节点中读取数据
std::string value = child.get<std::string>("SomeValue");
qDebug()<<value ;

xml
<Root>
    <ChildNodeName>
        <SomeValue>Example Value</SomeValue>
    </ChildNodeName>
</Root>
相关推荐
郝学胜_神的一滴21 小时前
Qt 高级开发 021:零基础吃透 QVBoxLayout 垂直布局
c++·qt
誰能久伴不乏1 天前
libmodbus 在 Windows 环境下报 “Invalid argument“ 的排错记录
c++·qt·modbus
小许同学记录成长1 天前
网格简化算法 — Edge Collapse(边塌缩)
qt·算法
tangchao340勤奋的老年?1 天前
遮挡式引导界面设计
qt
酬勤-人间道1 天前
VTK 与 Cesium-native 结合实践:小场景三维编辑 + 数字地球精准贴合
c++·qt·vtk·遥感·岩土·cesium-native
誰能久伴不乏1 天前
Qt C++ 解析 JSON 完全指南:从核心概念到工业级实战
c++·qt·json
XiYang-DING1 天前
【MyBatis】XML方式实现CRUD
xml·mybatis
雪的季节1 天前
Qt 高性能绘图的核心原理
qt
Irissgwe1 天前
一、Qt 概述
c++·qt·gui·qt creator
m0_617493941 天前
PySide6/PyQt6实现中英文切换完整教程(Qt Designer + Qt Linguist + 动态切换)
开发语言·qt