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>
相关推荐
十启树2 小时前
用Qt 对接‌百度AI平台
人工智能·qt·百度
DS小龙哥2 小时前
QT For Android开发-打开PPT文件
android·qt·powerpoint
锦亦之223312 小时前
QT+OSG+OSG-earth如何在窗口显示一个地球
开发语言·qt
柳鲲鹏15 小时前
编译成功!QT/6.7.2/Creator编译Windows64 MySQL驱动(MinGW版)
开发语言·qt·mysql
三玖诶15 小时前
如何在 Qt 的 QListWidget 中逐行添加和显示数据
开发语言·qt
阳光开朗_大男孩儿21 小时前
DBUS属性原理
linux·服务器·前端·数据库·qt
Alphapeople1 天前
Qt Modbus
开发语言·qt
竹林海中敲代码1 天前
Qt Creator 集成开发环境 常见问题
qt·qt工具常见问题
竹林海中敲代码1 天前
Qt安卓开发连接手机调试(红米K60为例)
android·qt·智能手机
长沙红胖子Qt1 天前
关于 Qt运行加载内存较大崩溃添加扩大运行内存 的解决方法
开发语言·qt·qt扩大运行内存