解析Xml文件并修改QDomDocument的值

背景:

我需要解决一个bug,需要我从xml中读取数据到QDomDocument,然后获取到我想要的目标信息,然后修改该信息。 

---------------------------------------------------------------------------------------------------------

Qt QDomComment读写Xml文件(含示例源码)_qt qdomdocument 操做xml-CSDN博客

QDomDocument类可以方便地读取和操作XML文件。优点:易于使用XML文档,缺点是相对较慢。

---------------------------------------------------------------------------------------------------

示例Xml文档 

XML 教程 | 菜鸟教程 (runoob.com)

<class><student sex="男" age="18"><id>01</id><name>张三</name></student><student sex="女" age="28"><id>02</id><name>李四</name></student>	
</class>

标签

标签内包含了要传递的信息

它必须成对出现,有开始标签就需要有结束标签。

Xml文件必须包含根元素,它是所有其它元素的父元素

XML文档由元素构成,每个元素包含开始标签,结束标签和元素内容。

例如:<id>02</id>

读取Xml文件的信息

        //XML文件路径QString path = QCoreApplication::applicationDirPath()+"/xxx.xml";qDebug()<<path;//打开XML文件QFile file(path);if(!file.open(QIODevice::ReadOnly)){qDebug()<<"File open faild!";return-1;}//创建QDomDocument对象,用于解析XMLQDomDocument doc;//通过QFile对象设置QDomDocument的内容if(!doc.setContent(&file)){file.close();return -1;}//关闭文件file.close();//解析XML//获取XML根结点QDomElement root = doc.documentElement();//遍历每个student结点//通过标签名获取结点列表QDomNodeList studentList = root.elementsByTagName("student");int listSize = studentList.size();for(int i = 0; i < listSize; ++i){//通过索引获取QDomElement对象QDomElement student = studentList.at(i).toElement();//获取sex属性值QString sex = student.attribute("sex");//获取age属性值QString age = student.attribute("age");/**函数原型为*QDomElement firstChildElement(const QString &tagName = QString()) const;*解析:参数tagName是可选的,表示要查询的子元素结点的标签名。如果指定了tagName,则返回*第一个匹配标签的子元素结点;如果没有指定tagName,则返回第一个子元素结点。**/QString studentID = student.firstChildElement("id").text();       //获取id元素节点的文本内容QString studentName = student.firstChildElement("name").text();   //获取name元素节点的文本内容qDebug()<<"student:";qDebug()<<"sex:"<<sex;qDebug()<<"age:"<<age;qDebug()<<"studentID:"<<studentID;qDebug()<<"studentName:"<<studentName;}

这里我继续使用Qt的.ui文件作为示例(它是以Xml文件的形式进行存储的)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>731</width><height>460</height></rect></property><property name="windowTitle"><string>Graphics  View绘图</string></property><widget class="QWidget" name="centralWidget"><widget class="QWGraphicsView" name="View"><property name="geometry"><rect><x>10</x><y>10</y><width>600</width><height>400</height></rect></property><property name="renderHints"><set>QPainter::Antialiasing|QPainter::TextAntialiasing</set></property><property name="dragMode"><enum>QGraphicsView::RubberBandDrag</enum></property></widget></widget><widget class="QMenuBar" name="menuBar"><property name="geometry"><rect><x>0</x><y>0</y><width>731</width><height>30</height></rect></property></widget><widget class="QToolBar" name="mainToolBar"><property name="iconSize"><size><width>16</width><height>16</height></size></property><property name="toolButtonStyle"><enum>Qt::ToolButtonTextUnderIcon</enum></property><attribute name="toolBarArea"><enum>TopToolBarArea</enum></attribute><attribute name="toolBarBreak"><bool>false</bool></attribute><addaction name="actZoomIn"/><addaction name="actZoomOut"/><addaction name="actRestore"/><addaction name="separator"/><addaction name="actRotateLeft"/><addaction name="actRotateRight"/><addaction name="actEdit_Front"/><addaction name="actEdit_Back"/><addaction name="actGroup"/><addaction name="actGroupBreak"/><addaction name="separator"/><addaction name="actEdit_Delete"/><addaction name="separator"/><addaction name="actQuit"/></widget><widget class="QStatusBar" name="statusBar"/><widget class="QToolBar" name="toolBar"><property name="windowTitle"><string>toolBar</string></property><property name="allowedAreas"><set>Qt::LeftToolBarArea</set></property><property name="iconSize"><size><width>16</width><height>16</height></size></property><property name="toolButtonStyle"><enum>Qt::ToolButtonTextUnderIcon</enum></property><attribute name="toolBarArea"><enum>LeftToolBarArea</enum></attribute><attribute name="toolBarBreak"><bool>false</bool></attribute><addaction name="actItem_Rect"/><addaction name="actItem_Ellipse"/><addaction name="actItem_Circle"/><addaction name="actItem_Triangle"/><addaction name="actItem_Polygon"/><addaction name="actItem_Line"/><addaction name="actItem_Text"/></widget><action name="actItem_Rect"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/RECTANGL.BMP</normaloff>:/images/images/RECTANGL.BMP</iconset></property><property name="text"><string>矩形</string></property><property name="toolTip"><string>添加矩形</string></property></action><action name="actItem_Ellipse"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/ELLIPSE.BMP</normaloff>:/images/images/ELLIPSE.BMP</iconset></property><property name="text"><string>椭圆</string></property><property name="toolTip"><string>添加椭圆型</string></property></action><action name="actItem_Line"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/LINE.BMP</normaloff>:/images/images/LINE.BMP</iconset></property><property name="text"><string>直线</string></property><property name="toolTip"><string>添加直线</string></property></action><action name="actEdit_Delete"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/108.bmp</normaloff>:/images/images/108.bmp</iconset></property><property name="text"><string>删除</string></property><property name="toolTip"><string>删除选中的图元</string></property></action><action name="actQuit"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/132.bmp</normaloff>:/images/images/132.bmp</iconset></property><property name="text"><string>退出</string></property><property name="toolTip"><string>退出本系统</string></property></action><action name="actItem_Text"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/800.bmp</normaloff>:/images/images/800.bmp</iconset></property><property name="text"><string>文字</string></property><property name="toolTip"><string>添加文字</string></property></action><action name="actEdit_Front"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/528.bmp</normaloff>:/images/images/528.bmp</iconset></property><property name="text"><string>前置</string></property><property name="toolTip"><string>居于最前面</string></property></action><action name="actEdit_Back"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/526.bmp</normaloff>:/images/images/526.bmp</iconset></property><property name="text"><string>后置</string></property><property name="toolTip"><string>居于最后面</string></property></action><action name="actItem_Polygon"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/FREEFORM.BMP</normaloff>:/images/images/FREEFORM.BMP</iconset></property><property name="text"><string>梯形</string></property><property name="toolTip"><string>添加梯形</string></property></action><action name="actZoomIn"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/zoomin.png</normaloff>:/images/images/zoomin.png</iconset></property><property name="text"><string>放大</string></property><property name="toolTip"><string>放大</string></property></action><action name="actZoomOut"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/zoomout.png</normaloff>:/images/images/zoomout.png</iconset></property><property name="text"><string>缩小</string></property><property name="toolTip"><string>缩小</string></property></action><action name="actRotateLeft"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/rotateleft.png</normaloff>:/images/images/rotateleft.png</iconset></property><property name="text"><string>左旋转</string></property><property name="toolTip"><string>左旋转</string></property></action><action name="actRotateRight"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/rotateright.png</normaloff>:/images/images/rotateright.png</iconset></property><property name="text"><string>右旋转</string></property><property name="toolTip"><string>右旋转</string></property></action><action name="actRestore"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/420.bmp</normaloff>:/images/images/420.bmp</iconset></property><property name="text"><string>恢复</string></property><property name="toolTip"><string>恢复大小</string></property></action><action name="actGroup"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/UNGROUP.BMP</normaloff>:/images/images/UNGROUP.BMP</iconset></property><property name="text"><string>组合</string></property><property name="toolTip"><string>组合</string></property></action><action name="actGroupBreak"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/128.bmp</normaloff>:/images/images/128.bmp</iconset></property><property name="text"><string>打散</string></property><property name="toolTip"><string>取消组合</string></property></action><action name="actItem_Circle"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/08.JPG</normaloff>:/images/images/08.JPG</iconset></property><property name="text"><string>圆形</string></property><property name="toolTip"><string>圆形</string></property></action><action name="actItem_Triangle"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/Icon1242.ico</normaloff>:/images/images/Icon1242.ico</iconset></property><property name="text"><string>三角形</string></property><property name="toolTip"><string>三角形</string></property></action></widget><layoutdefault spacing="6" margin="11"/><customwidgets><customwidget><class>QWGraphicsView</class><extends>QGraphicsView</extends><header location="global">qwgraphicsview.h</header></customwidget></customwidgets><resources><include location="res.qrc"/></resources><connections><connection><sender>actQuit</sender><signal>triggered()</signal><receiver>MainWindow</receiver><slot>close()</slot><hints><hint type="sourcelabel"><x>-1</x><y>-1</y></hint><hint type="destinationlabel"><x>302</x><y>153</y></hint></hints></connection></connections>
</ui>

还是有难度的。

最外层是标签ui

内部是

class

widget

layoutdefault

customwidgets

resources

connections

------------------------------------------------

widget内包含

property

widget

action

--------------------------------------------------------

我读取一下:

  <property name="geometry"><rect><x>0</x><y>0</y><width>731</width><height>460</height></rect></property><property name="windowTitle"><string>Graphics  View绘图</string></property>

这一段信息吧

读取.ui文件的代码

        //解析XMLQDomNode firstNode = doc.firstChild();qDebug()<<"版本和编码信息:"<<firstNode.nodeName()<<firstNode.nodeValue(); // "xml" "version='1.0' encoding='UTF-8'"qDebug()<<"是否是Xml说明:"<<firstNode.isProcessingInstruction();QDomElement ui = doc.documentElement();qDebug()<<"root tag:"<<ui.tagName();qDebug()<<"root nodeType:"<<ui.nodeType();    //QDomNode::ElementNode	1qDebug()<<"root hasChildNodes:"<<ui.hasChildNodes();    //trueqDebug()<<"root childNodes count:"<<ui.childNodes().count();    //6qDebug()<<"root hasAttributes:"<<ui.hasAttributes();    //trueqDebug()<<"root hasAttribute version:"<<ui.hasAttribute("version"); //trueQDomElement xWidget = ui.firstChildElement("widget");qDebug()<<"widget hasAttribute name:"<<xWidget.hasAttribute("name");    //trueqDebug()<<"widget hasAttribute class:"<<xWidget.hasAttribute("class");  //trueQDomElement xProperty = xWidget.firstChildElement("property");while(!xProperty.isNull()){qDebug()<<xProperty.attribute("name");xProperty = xProperty.nextSiblingElement("property");}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/372258.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

后端之路——登录校验前言(Cookie\ Session\ JWT令牌)

前言&#xff1a;Servlet 【登录校验】这个功能技术的基础是【会话技术】&#xff0c;那么在讲【会话技术】的时候必然要谈到【Cookie】和【Session】这两个东西&#xff0c;那么在这之前必须要先讲一下一个很重要但是很多人都会忽略的一个知识点&#xff1a;【Servlet】 什么是…

STM32-外部中断浅析

本篇解释了STM32中断原理 MCU为什么需要中断 中断&#xff0c;是嵌入式系统中很重要的一个功能&#xff0c;在系统运行过程中&#xff0c;当出现需要立刻处理的情况时&#xff0c;暂停当前任务&#xff0c;转而处理紧急任务&#xff0c;处理完毕后&#xff0c;恢复之前的任务…

vue3项目图片压缩+rem+自动重启等plugin使用与打包配置

一、Svg配置 每次引入一张 SVG 图片都需要写一次相对路径&#xff0c;并且对 SVG 图片进行压缩优化也不够方便。 vite-svg-loader插件加载SVG文件作为Vue组件&#xff0c;使用SVGO进行优化。 插件网站https://www.npmjs.com/package/vite-svg-loader 1. 安装 pnpm i vite-svg…

谷粒商城学习笔记-使用renren-fast-vue框架时安装依赖包遇到的问题及解决策略

文章目录 1&#xff0c;npm error Class extends value undefined is not a constuctor or null2&#xff0c;npm warn cli npm v10.8.1 does not support Node.js v16.20.2.3&#xff0c;npm error code CERT_HAS_EXPIRED学习心得 这篇文章记录下使用renren-fast-vue&#xff…

Unity3D游戏 RPG

丛林探险游戏 人物进行探险游戏 拥有登录&#xff0c;首页&#xff0c;3D物体旋转浏览的功能&#xff0c;还能进行种植树等功能

11 个例子讲清spark提交命令参数

目录 提交命名参数详情为什么有这么多参数如何开始学习一些具体的例子1. 基本的Spark应用提交2. 提交带有依赖的Python脚本3. 运行Spark SQL作业4. 提交Spark Streaming作业5. 使用外部包运行Spark作业6. 动态资源分配7. 使用多个配置文件8. GPU 支持9. 自定义日志配置10. 使用…

swiftui中NavigationStack布局navigationBarTitleDisplayMode作用,以及内容顶部空白区域解决办法

写了一个小demo用于学习NavigationStack和toolbar/ToolbarItem知识&#xff0c;但是在写一个瀑布流布局的时候&#xff0c;设置了顶部的toolbar&#xff0c;然后内容区域的顶部出现了一大片空白区域&#xff0c;这样的效果并不是很美观很好看&#xff0c;所以就想着研究解决一下…

人工智能的新时代:从模型到应用的转变

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

Chrome 127内置AI大模型攻略

Chrome 127 集成Gemini:本地AI功能 Google将Gemini大模型整合进Chrome浏览器,带来全新免费的本地AI体验: 完全免费、无限制使用支持离线运行,摆脱网络依赖功能涵盖图像识别、自然语言处理、智能推荐等中国大陆需要借助魔法,懂都懂。 安装部署步骤: 1. Chrome V127 dev …

城市地下综合管廊物联网远程监控

城市地下综合管廊物联网远程监控 城市地下综合管廊&#xff0c;作为现代都市基础设施的重要组成部分&#xff0c;其物联网远程监控系统的构建是实现智慧城市建设的关键环节。这一系统集成了先进的信息技术、传感器技术、通信技术和数据处理技术&#xff0c;旨在对埋设于地下的…

数据分析与挖掘实战案例-电商产品评论数据情感分析

数据分析与挖掘实战案例-电商产品评论数据情感分析 文章目录 数据分析与挖掘实战案例-电商产品评论数据情感分析1. 背景与挖掘目标2. 分析方法与过程2.1 评论预处理1. 评论去重2. 数据清洗 2.2 评论分词1. 分词、词性标注、去除停用词2. 提取含名词的评论3. 绘制词云查看分词效…

Java---包装类与泛型

1.包装类 1.1 包装类 在Java中&#xff0c;由于基本数据类型不是继承Object类&#xff0c;为了在泛型代码中可以支持基本数据类型&#xff0c;Java给每个基本数据类型各自提供了一个包装类。 如下图 除了char和int基本数据类型的包装类型有点特别&#xff0c;其他的都是首字…

MySQL Binlog详解:提升数据库可靠性的核心技术

文章目录 1. 引言1.1 什么是MySQL Bin Log&#xff1f;1.2 Bin Log的作用和应用场景 2. Bin Log的基本概念2.1 Bin Log的工作原理2.2 Bin Log的三种格式 3. 配置与管理Bin Log3.1 启用Bin Log3.2 配置Bin Log参数3.3 管理Bin Log文件3.4 查看Bin Log内容3.5 使用mysqlbinlog工具…

LabVIEW自动探头外观检测

开发了一套基于LabVIEW的软件系统&#xff0c;结合视觉检测技术&#xff0c;实现探头及连接器外观的自动检测。通过使用高分辨率工业相机、光源和机械手臂&#xff0c;系统能够自动定位并检测探头表面的细微缺陷&#xff0c;如划痕、残胶、异色、杂物等。系统支持多种探头形态&…

栈 栈是一种数据结构&#xff0c;只允许在固定一端进行插入和删除功能&#xff0c;进行插入和删除的一端叫做栈顶&#xff0c;另一端叫做栈底&#xff0c;遵循后入先出的规则&#xff0c;就像穿烤串和吃烤串一样 其中&#xff0c;插入数据叫做进栈/压栈/入栈&#xff0c;数据插…

Nacos 进阶篇---集群:选举心跳健康检查劳动者(九)

一、引言 本章将是我们第二阶段&#xff0c;开始学习集群模式下&#xff0c;Nacos 是怎么去操作的 &#xff1f; 本章重点&#xff1a; 在Nacos服务端当中&#xff0c;会去开启健康心跳检查定时任务。如果是在Nacos集群下&#xff0c;大家思考一下&#xff0c;有没有必要所有的…

MySQL存储过程创建

DQL call create_order_infos(7,2,3); delimiter $$ CREATE PROCEDURE create_order_infos( in in_user_id int, in in_product_id int, in in_count int ) BEGIN -- 业务逻辑 SELECT in_user_id 用户id,in_product_id 产品id,in_count 购买数量; end $$ delimiter ; 结果 c…

7.8洛谷 字符串

P5650 基础字符串练习题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 思路 如果 S[i] 0&#xff0c;则 dp[i] max(dp[i-1] 1, 1)&#xff08;因为增加了 0&#xff0c;减少了 1&#xff09;。如果 S[i] 1&#xff0c;则 dp[i] max(dp[i-1] - 1, -1)&#xff08;因为减…

九浅一深Jemalloc5.3.0 -- ④浅*配置

目前市面上有不少分析Jemalloc老版本的博文&#xff0c;但最新版本5.3.0却少之又少。而且5.3.0的架构与5之前的版本有较大不同&#xff0c;本着“与时俱进”、“由浅入深”的宗旨&#xff0c;我将逐步分析最新release版本Jemalloc5.3.0的实现。 另外&#xff0c;单讲实现代码是…

这几类人,千万不要买纯电车

文 | AUTO芯球 作者 | 响铃 纯电车的冤大头真是太多了&#xff0c; 我之前劝过&#xff0c;有些人不适合买纯电车&#xff0c; 你们看&#xff0c;果然吧&#xff0c;麦卡锡最近的一份报告就披露了 去年啊&#xff0c;22%的人在买了电车后后悔了&#xff0c; 这些人说了&a…