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>
相关推荐
道尔柯南13 分钟前
【QT】从零认识 Qt:跨平台 C++ GUI 开发入门
开发语言·c++·qt
MayZork38 分钟前
std::filesystem 文件操作详解
开发语言·c++·qt
kaixin_learn_qt_ing1 小时前
[银河麒麟]因为没有安装串口相关驱动,导致无法检测到对应的串口,后来安装后,我的qt的相关接口就可以识别到了
qt
Cx330❀19 小时前
Qt 多线程深度解析:从底层原理到 UI 线程与同步实战
开发语言·qt·ui·搜索引擎·性能优化·图形渲染
西西弗Sisyphus19 小时前
Qt6 实现 会动的界面像物理世界一样
c++·qt
MayZork20 小时前
Qt 开发集成CMake + vcpkg
开发语言·c++·qt
书生执笔画浮沉20 小时前
Linux桌面端应用向鸿蒙PC迁移适配纪实
c++·qt·c·信创·海思·libc·ohos
今夜有雨.21 小时前
图像运算、掩膜/ROI 与绘制交互
c语言·数据结构·c++·qt·算法·计算机视觉
时间的拾荒人1 天前
Qt 网络编程与音视频开发实战:从 UDP/TCP 到 HTTP 与多媒体
网络·qt·音视频
扶尔魔ocy2 天前
【qt openGL入门笔记】FPS相机
qt·opengl