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>
相关推荐
NiNi_suanfa3 小时前
【Qt】Qt 批量修改同类对象
开发语言·c++·qt
mengzhi啊4 小时前
c++11的变参模板和c++17的左折叠
qt
路痴楷5 小时前
无法定位程序输入点问题
c++·qt·visual studio
Source.Liu6 小时前
【LibreCAD】 RS_Units 类完整解析
c++·qt·rust
偶像你挑的噻7 小时前
2.Qt-基础核心以及信号与槽
开发语言·qt
爱吃巧克力的程序媛11 小时前
Qt 异步编程---概述
开发语言·qt
编程修仙12 小时前
第十一篇 Spring事务
xml·java·数据库·spring
天涯路s12 小时前
qt怎么自定义日志
开发语言·qt
金士顿14 小时前
Ethercat耦合器添加的IO导出xml 初始化IO参数
android·xml·java
四维碎片14 小时前
【Qt】QTimer 学习笔记总结
笔记·qt·学习