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>
相关推荐
丰锋ff9 小时前
基于 Qt 的智能门禁系统(二)
开发语言·qt
进击ing小白14 小时前
Qt 无边框窗口进阶指南
开发语言·qt
问君能有几多愁~1 天前
Qt Json 序列化操作
开发语言·qt·json
平常心的技术小牛1 天前
Qt-快速上手-QFileDialog
windows·qt·microsoft
小小龙学IT1 天前
Qt Graphics View Framework(图形视图框架)深度解析:从 Scene/View/Item 到工业级 2D 场景
开发语言·数据库·qt
程与留1 天前
09_Qt 布局管理系统——QHBoxLayout、QVBoxLayout、QGridLayout
c++·qt
神仙别闹1 天前
基于QT(C++)实现旅游线路规划系统
c++·qt·旅游
≮傷£≯√1 天前
音量调节弹窗 自定义组件qt
开发语言·qt
小小龙学IT1 天前
Qt 6 跨平台开发完全指南:从信号槽机制到实际项目落地
开发语言·qt
丰锋ff2 天前
基于 Qt 的智能门禁系统
qt·学习