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>
相关推荐
SunkingYang7 小时前
QT中使用Lambda表达式作为槽函数用法,以及捕获列表和参数列表用法与区别
c++·qt·用法·lambda表达式·捕获列表·槽函数·参数列表
千疑千寻~11 小时前
【Qt图形】详细介绍Qt中的图形场景、图元、视图
qt
千疑千寻~12 小时前
【Qt图形】Qt中的图形场景、图元、视图 的坐标的转换
qt
iSee85714 小时前
struts2 XML外部实体注入漏洞复现(CVE-2025-68493)
xml·安全·struts2
一叶之秋141221 小时前
Qt常用控件(三)
开发语言·qt
_OP_CHEN21 小时前
【从零开始的Qt开发指南】(二十三)Qt 界面优化之绘图实战:解锁自定义界面的无限可能
开发语言·qt
郝学胜-神的一滴21 小时前
使用QVideoWidget实现高效视频播放:从基础到高级应用
开发语言·c++·qt·程序人生·音视频
叶之道21 小时前
MacOS 配置 Qt 开发环境
qt·macos
爱吃泡芙的小白白21 小时前
Qt 3D老树新花:为何在工业与车载领域仍是首选?
开发语言·qt·3d
余衫马21 小时前
Qt for Python:PySide6 入门指南
开发语言·c++·python·qt