解析Xml文件并修改QDomDocument的值

news2024/9/21 12:38:17

背景:

我需要解决一个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对象,用于解析XML
        QDomDocument 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文件的代码

        //解析XML
        QDomNode 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	1
        qDebug()<<"root hasChildNodes:"<<ui.hasChildNodes();    //true
        qDebug()<<"root childNodes count:"<<ui.childNodes().count();    //6
        qDebug()<<"root hasAttributes:"<<ui.hasAttributes();    //true
        qDebug()<<"root hasAttribute version:"<<ui.hasAttribute("version"); //true

        QDomElement xWidget = ui.firstChildElement("widget");
        qDebug()<<"widget hasAttribute name:"<<xWidget.hasAttribute("name");    //true
        qDebug()<<"widget hasAttribute class:"<<xWidget.hasAttribute("class");  //true

        QDomElement xProperty = xWidget.firstChildElement("property");
        while(!xProperty.isNull())
        {
            qDebug()<<xProperty.attribute("name");
            xProperty = xProperty.nextSiblingElement("property");
        }

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

PPO控制人形机器人行走举例

PPO控制人形机器人行走 Proximal Policy Optimization (PPO) 是一种策略优化算法,在强化学习中广泛使用。它通过改进策略梯度方法,使得训练过程更加稳定和高效。 PPO算法原理介绍 PPO算法主要有两种变体:PPO-Clip 和 PPO-Penalty。这里主要介绍PPO-Clip,因为它更常用。 …

RecyclerView

1、导入RecyclerView包 2、在activity_main.xml中创建RecyclerView <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"…

Face_recognition实现人脸识别

这里写自定义目录标题 欢迎使用Markdown编辑器一、安装人脸识别库face_recognition1.1 安装cmake1.2 安装dlib库1.3 安装face_recognition 二、3个常用的人脸识别案例2.1 识别并绘制人脸框2.2 提取并绘制人脸关键点2.3 人脸匹配及标注 欢迎使用Markdown编辑器 本文基于face_re…

@Builder注解详解:巧妙避开常见的陷阱

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 &#x1f38f;&#xff1a;你只管努力&#xff0c;剩下的交给时间 &#x1f3e0; &#xff1a;小破站 Builder注解详解&#xff1a;巧妙避开常见的陷阱 前言1. Builder的基本使用使用示例示例类创建对…

Java——面试题

1、JDK 和 JRE 有什么区别&#xff1f; JDK&#xff08;Java Development Kit&#xff09;&#xff0c;Java开发工具包 JRE&#xff08;Java Runtime Environment&#xff09;&#xff0c;Java运行环境 JDK中包含JRE&#xff0c;JDK中有一个名为jre的目录&#xff0c;里面包含…

电子发票管理系统-计算机毕业设计源码99719

摘 要 本文旨在设计和实现一个基于SpringBoot的电子发票管理系统&#xff0c;以提升企业的发票管理效率和准确性。随着电子化发票管理的需求增加&#xff0c;企业需要一个高效、可靠且功能丰富的系统来帮助管理发票信息。基于SpringBoot的电子发票管理系统将提供诸如发票信息、…

多数据源及其连接池的配置、事务管理器的注册和使用

&#xff08;ps&#xff1a;如果只有这几个数据源&#xff0c;请选择一个默认的数据源和对应的事务管理器均加上Primary注解&#xff09;示例&#xff1a; 1.在yml文件中配置多数据源/池的信息 spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:initia…

nodejs + vue3 模拟 fetchEventSouce进行sse流式请求

先上效果图: 前言: 在GPT爆发的时候,各项目都想给自己的产品加上AI,蹭上AI的风口,因此在最近的一个需求,就想要给项目加入Ai的功能,原本要求的效果是,查询到对应的数据后,完全展示出来,也就是常规的post请求,后来这种效果遇到了一个很现实的问题:长时间的等待。我…

SCI三区|儿童学习优化算法KLO:基于社会进化和认知学习的优化算法

目录 1.背景2.算法原理2.1算法思想2.2算法过程 3.结果展示4.参考文献5.代码获取 1.背景 2024年&#xff0c;ST Javed受到社会环境下家庭儿童的早期社会学习行为启发&#xff0c;提出了儿童学习优化算法&#xff08;Kids Learning Optimizer, KLO&#xff09;。 2.算法原理 2.…

使用MySQLInstaller配置MySQL

操作步骤 1.配置High Availability 默认选项Standalone MySQL Server classic MySQL Replication 2.配置Type and Networking ◆端口默认启用TCP/P网络 ◆端口默认为3306 3.配置Account and Roles 设置root账户的密码、添加其他管理员 4.配置Windows Service ◆配置MySQL Serv…

day4单向链表

主程序 #include "fun.h" int main(int argc, const char *argv[]) { node_p Lcreate_head();//创建链表 printf("########################链表的头插尾插\n"); insert_head(L,45);//头插 insert_head(L,45); insert_tail(L,45);/…

imx6ull/linux应用编程学习(14) MQTT基础知识

什么是mqtt&#xff1f; 与HTTP 协议一样&#xff0c; MQTT 协议也是应用层协议&#xff0c;工作在 TCP/IP 四层模型中的最上层&#xff08;应用层&#xff09;&#xff0c;构建于 TCP/IP协议上。 MQTT 最大优点在于&#xff0c;可以以极少的代码和有限的带宽&#xff0c;为连接…

极客时间:使用Autogen Builder和本地LLM(Microsoft Phi3模型)在Mac上创建本地AI代理

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

Jenkins教程-15-常用插件-Blue Ocean

上一小节我们学习了Jenkins定时任务构建的方法&#xff0c;本小节我们讲解一下Jenkins常用插件Blue Ocean的使用方法。 Blue Ocean 提供了一套可视化操作界面来帮助创建、编辑 Pipeline 任务。 Blue Ocean 特性&#xff1a; 流水线编辑器&#xff1a;用于创建贯穿始终的持续交…

JavaScript学习笔记(七)

45.9 JavaScript 可迭代对象 可迭代对象&#xff08;Iterables&#xff09;是可以使用 for..of 进行迭代的对象。 从技术上讲&#xff0c;可迭代对象必须实现 Symbol.iterator 方法。 45.9.1 遍历字符串 <body><p id"demo"></p><script>c…

关于centos7自带的nginx1.20.1开启https后,XP系统的IE6和IE8无法显示网页的问题

CentOS7自带的nginx-1.20.1是支持HTTP/2和TLS1.3的。 软件包名称&#xff1a;nginx-1.20.1-10.el7.x86_64 CentOS7默认开启了HTTP/2&#xff0c;但没有开启TLS1.3&#xff0c;以及IE6和IE8的https访问。 开启方法&#xff1a; ssl_ciphers HIGH:!aNULL:!MD5;改为ssl_ciphers…

1-3分钟爆款视频素材在哪找啊?这9个热门爆款素材网站分享给你

在如今快节奏的时代&#xff0c;短视频已成为吸引观众注意力的黄金手段。然而&#xff0c;要制作出1-3分钟的爆款视频&#xff0c;除了创意和剪辑技巧外&#xff0c;选择合适的素材至关重要。那么&#xff0c;哪里可以找到那些能让你的视频脱颖而出的爆款素材呢&#xff1f;不用…

【UE5.1】Chaos物理系统基础——05 蓝图绑定Chaos破裂或碰撞事件

步骤 1. 新建一个父类为Actor的蓝图&#xff0c;这里命名为“BP_ChaosExplosionEvent” 打开“BP_ChaosExplosionEvent”&#xff0c;添加一个变量&#xff0c;这里命名为“GC”&#xff0c;变量类型为“几何体集actor”&#xff0c;设置为可编辑实例 在事件图表中添加如下节点…

ELK+Filebeat+Kafka+Zookeeper

本实验基于ELFK已经搭好的情况下 ELK日志分析 架构解析 第一层、数据采集层 数据采集层位于最左边的业务服务器集群上&#xff0c;在每个业务服务器上面安装了filebeat做日志收集&#xff0c;然后把采集到的原始日志发送到Kafkazookeeper集群上。第二层、消息队列层 原始日志发…

通过端口转发实现docker容器运行时端口更改

通过端口转发实现docker容器运行时端口更改 前言启动容器查看容器ip地址端口转发 前言 关于修改docker正在运行中容器端口&#xff0c;网上大部分分为3类: 1. 删除原有容器重新创建;2. 改配置文件;3. 在现有容器上新提交镜像&#xff0c;用新镜像起新的容器。 1和3属于同一种流…