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>
相关推荐
行十万里人生14 小时前
Qt事件处理:理解处理器、过滤器与事件系统
开发语言·git·qt·华为od·华为·华为云·harmonyos
黑金IT14 小时前
Python3 + Qt5:实现AJAX异步更新UI
qt·ui·ajax
人工智能教学实践15 小时前
基于 yolov8_pyqt5 自适应界面设计的火灾检测系统 demo:毕业设计参考
qt·yolo·课程设计
扎量丙不要犟16 小时前
跨平台的客户端gui到底是选“原生”还是web
前端·javascript·c++·qt·rust·electron·tauri
笑鸿的学习笔记2 天前
qt-Quick3D笔记之官方例程Runtimeloader Example运行笔记
笔记·qt·3d
菜一头包2 天前
线程池以及在QT中的接口使用
c++·qt
R三哥哥啊2 天前
【Qt5】声明之后快速跳转
开发语言·qt·qt5
深蓝海拓2 天前
使用QSqlQueryModel创建交替背景色的表格模型
数据库·qt·pyqt
冰激凌zz2 天前
QT TLS initialization failed
qt
宁静致远20212 天前
Qt u盘自动升级软件
数据库·qt·嵌入式linux开发