qt中xml的读取

引言

该文章是基于上一篇文章,xml生成而写的,其所用的xml文件的内容是上一篇中的内容。

示例1

文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<server><helperIp>127.0.0.1</helperIp><sendPort>8900</sendPort><receviePort>9666</receviePort><kserver>127.0.0.1</kserver><kPort>9080</kPort><upServer>127.0.0.1</upServer><upPort>7999</upPort>
</server>

读xml文件

const QString strSourceConfigFileName = "/serversInfo.xml";void Config::readServerInfoFile()
{QString strPath = getExeFilePath();strPath += strSourceConfigFileName;QFile file(strPath);if (!file.open(QIODevice::ReadOnly)) {//之前的内容被清空qDebug()<<QStringLiteral("打开")<<strPath<<QStringLiteral("文件失败!");return;}QDomDocument doc;if (!doc.setContent(&file)) {qDebug()<<QStringLiteral("文件")<<strPath<<QStringLiteral("解析失败!");file.close();return ;}file.close();QDomElement root = doc.documentElement();QDomNode node = root.firstChild();while (!node.isNull()) {if (node.isElement()) {QDomElement serverElement = node.toElement();if (serverElement.tagName() == "helperIp") {m_localIP = serverElement.text();}else if (serverElement.tagName() == "sendPort") {m_localSendMsgPort = serverElement.text().toUInt();}else if (serverElement.tagName() == "receviePort") {m_localRecevieMsgPort = serverElement.text().toUInt();}else if (serverElement.tagName() == "kserver") {m_kvmIP = serverElement.text();}else if (serverElement.tagName() == "kPort") {m_kvmPort = serverElement.text().toUInt();}else if (serverElement.tagName() == "upServer") {m_upScreenIp = serverElement.text();}else if (serverElement.tagName() == "upPort") {m_upScreenPort = serverElement.text().toUInt();}}node = node.nextSibling();}
}

上面的读取的文件保存的变量的声明如下:

   QString                     m_localIP;//本地IP,与助手通信的IPquint16                     m_localRecevieMsgPort;//中间件接收助手消息的端口quint16                     m_localSendMsgPort;//中间件发送到助手消息的端口QString                     m_kvmIP;//kvm的IPquint16                     m_kvmPort;//kvm的端口QString                     m_upScreenIp;//上屏的Ipquint16                     m_upScreenPort;//上屏的端口

示例2

文件内容:

<?xml version='1.0' encoding='UTF-8'?>
<kvm><seatInfo><seat><sid>12</sid><seatName>席位1</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.32</seatIp></seat><seat><sid>13</sid><seatName>席位2</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.34</seatIp></seat><seat><sid>14</sid><seatName>席位3</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.22</seatIp></seat><seat><sid>15</sid><seatName>席位4</seatName><seatId>12289832899feds</seatId><seatIp>192.168.1.23</seatIp></seat><seat><sid>16</sid><seatName>席位5</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.24</seatIp></seat><seat><sid>17</sid><seatName>席位6</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.25</seatIp></seat><seat><sid>18</sid><seatName>席位7</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.26</seatIp></seat><seat><sid>19</sid><seatName>席位8</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.27</seatIp></seat><seat><sid>20</sid><seatName>席位9</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.28</seatIp></seat><seat><sid>21</sid><seatName>席位10</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.29</seatIp></seat><seat><sid>22</sid><seatName>席位11</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.30</seatIp></seat><seat><sid>23</sid><seatName>席位12</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.31</seatIp></seat><seat><sid>24</sid><seatName>席位13</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.32</seatIp></seat><seat><sid>25</sid><seatName>席位14</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.33</seatIp></seat><seat><sid>26</sid><seatName>席位15</seatName><seatId>12289832899desa</seatId><seatIp>192.168.1.34</seatIp></seat></seatInfo><signalsource><source><uid>1</uid><ip>192.168.1.21</ip></source><source><uid>2</uid><ip>192.168.1.22</ip></source><source><uid>3</uid><ip>192.168.1.23</ip></source><source><uid>4</uid><ip>192.168.1.24</ip></source><source><uid>5</uid><ip>192.168.1.25</ip></source><source><uid>6</uid><ip>192.168.1.26</ip></source><source><uid>7</uid><ip>192.168.1.27</ip></source><source><uid>8</uid><ip>192.168.1.28</ip></source><source><uid>9</uid><ip>192.168.1.29</ip></source><source><uid>10</uid><ip>192.168.1.30</ip></source></signalsource>
</kvm>

读xml文件

void Config::readKvmSeatSourceFile()
{QString strPath = getExeFilePath();strPath += strKvmConfigFileName;QFile file(strPath);if (!file.open(QIODevice::ReadOnly)) {qDebug()<<QStringLiteral("打开")<<strPath<<QStringLiteral("文件失败!");return;}QDomDocument doc;if (!doc.setContent(&file)) {//读xml文件,返回true文件被成功解析qDebug()<<QStringLiteral("文件")<<strPath<<QStringLiteral("解析失败!");file.close();return ;}file.close();QDomElement root = doc.documentElement();//根节点QDomNode firstNode = root.firstChild();//第一个子节点while (!firstNode.isNull()) {if (firstNode.isElement()) {QDomElement element = firstNode.toElement();if (element.tagName() == "seatInfo") {parseSeatInfo(element);}else if (element.tagName() == "signalsource") {parseSignalSource(element);}}firstNode = firstNode.nextSibling();}
}void Config::parseSeatInfo(QDomElement &element)
{if (m_seatSetList.size() != 0) {m_seatSetList.clear();}QDomNodeList nodeList = element.childNodes();for (int i = 0; i < nodeList.count(); ++i) {QDomNode node = nodeList.at(i);if (!node.isNull()) {if (node.isElement()) {QDomElement seatElement = node.toElement();if (seatElement.tagName() == "seat") {parseDetailedSeat(seatElement);}}}}
}void Config::parseSignalSource(QDomElement &element)
{if (m_sourceSetList.size() != 0) {m_sourceSetList.clear();}QDomNodeList nodeList = element.childNodes();for (int i = 0; i < nodeList.count(); ++i) {QDomNode node = nodeList.at(i);if (!node.isNull()) {if (node.isElement()) {QDomElement sourceElement = node.toElement();if (sourceElement.tagName() == "source") {parseDetailedSource(sourceElement);}}}}
}void Config::parseDetailedSource(QDomElement &element)
{stuSource tempSource;QDomNodeList nodeList = element.childNodes();for (int i = 0; i < nodeList.size(); ++i) {QDomNode node = nodeList.at(i);if (!node.isNull()) {if (node.isElement()) {QDomElement sourceInfoElement = node.toElement();if (sourceInfoElement.tagName() == "uid") {tempSource.uid = sourceInfoElement.text().toInt();}else if (sourceInfoElement.tagName() == "ip") {tempSource.ip = sourceInfoElement.text();}}}}m_sourceSetList.append(tempSource);
}void Config::parseDetailedSeat(QDomElement &element)
{stuSeat tempSeat;QDomNodeList nodeList = element.childNodes();for (int i = 0; i < nodeList.size(); ++i) {QDomNode node = nodeList.at(i);if (!node.isNull()) {if (node.isElement()) {QDomElement seatInfoElement = node.toElement();if (seatInfoElement.tagName() == "sid") {tempSeat.sid = seatInfoElement.text().toInt();}else if (seatInfoElement.tagName() == "seatName") {tempSeat.seatName = seatInfoElement.text();}else if (seatInfoElement.tagName() == "seatId") {tempSeat.seatId = seatInfoElement.text();}else if (seatInfoElement.tagName() == "seatIp") {tempSeat.seatIp = seatInfoElement.text();}}}}m_seatSetList.append(tempSeat);
}

最后用来储存数据的声明如下:

    QList<stuSeat>              m_seatSetList;//配置文件中读取设置的席位信息QList<stuSource>            m_sourceSetList;//配置文件读取的信号源信息```其它涉及的数据结构不在细说,读者明白其如何去读就行。

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

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

相关文章

第一次更名为OpenInfra的“她”,给我们带来了哪些惊喜?| 技术头条

戳蓝字“CSDN云计算”关注我们哦&#xff01;技术头条&#xff1a;干货、简洁、多维全面。更多云计算精华知识尽在眼前&#xff0c;get要点、solve难题&#xff0c;统统不在话下&#xff01;在美国丹佛阴冷的天气中&#xff0c;开源业界最顶级的技术峰会&#xff0c;前身为Open…

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? ide

mvn clean install -X -Dmaven.test.skiptrue -P dev 打包报错&#xff1a; No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? idea,项目&#xff0c;maven 也设置了统一的jdk,还是报错&#xff0c; 解决方法1&#xff…

qt中xml文件的更新

引言 这篇文章与其说是xml文件的更新&#xff0c;不如说是修改xml文件中之前的某些值。 示例 该文章依旧是基于之前的读写xml文章的基础,找到xml文件中每个节点的ip&#xff0c;查找最新的数据中是否含有xml文件中该节点的ip,有 则更新其名称和id. void Config::updateKvmS…

稳居亚太第一,阿里云进军全球;李彦宏候选工程院院士;苹果巨大损失:工业设计核心元老纷纷离职……...

关注并标星星CSDN云计算极客头条&#xff1a;速递、最新、绝对有料。这里有企业新动、这里有业界要闻&#xff0c;打起十二分精神&#xff0c;紧跟fashion你可以的&#xff01;每周三次&#xff0c;打卡即read更快、更全了解泛云圈精彩newsgo go go 【5月1日 星期三】云の声音不…

Guns 切换Oracle分支

远程拉取Guns代码 git clone gitgitee.com:gb_90/guns.git进入本地拉取Guns代码的目录下面 cd guns/切换到oracle分支 git checkout oracle切换到未删除Oracle配置的历史版本 提交id号是 &#xff1a;24466d6 git reset --hard 24466d6查看&#xff1b; dir导入idea开发工具效果…

QT中字符串分割

引言 字符串“<ew,we,ewe,sd,sd,sd>”&#xff0c;当求每一个字段的值时&#xff0c;可以采用字符串分割的方式来实现。 使用示例 下面只记录关键函数split. 使用QString的分割函数split来分割字符串&#xff1a; QString str “<ew,we,ewe,sd,sd,sd>”; QStr…

ZStack张鑫:面对混合云浪潮 我们主动出击

戳蓝字“CSDN云计算”关注我们哦&#xff01;极客头条&#xff1a;速递、最新、绝对有料。这里有企业新动、这里有业界要闻&#xff0c;打起十二分精神&#xff0c;紧跟fashion你可以的&#xff01;人类首次捕获黑洞照片&#xff0c;让我们看到了原本幽暗的宇宙&#xff0c;也有…

MyBatis-Plus_简介

第 1 章 简介 MyBatisPlus 介绍 MyBatis-Plus&#xff08;简称 MP&#xff09;是一个 MyBatis 的增强工具&#xff0c;在 MyBatis 的基础上只做增强不做改变&#xff0c;为简化开发、提高效率而生。 代码及文档发布地址 官方地址: http://mp.baomidou.com 代码发布地址: G…

qt中记录数据的一次接收处理方法

引言 在udp通信中&#xff0c;发送请求后&#xff0c;等待接收端的响应&#xff0c;有时一个回应被分割成好几次发送&#xff0c;这种反馈的形式出现带有随机性&#xff0c;正常的情况下是一次反馈所有的数据&#xff0c;但是会出现一次反馈被分成好几次反馈。像下面这样&…

解决ORA-00257: 归档程序错误。在释放之前仅限于内部连接

文章目录一、预先准备1. 现象2. 异常信息3. 分析二、解决方案2.1. 登录oracle2.2. 查看ARCHIVED LOG空间占用率2.3. 查看元空间大小2.4. 整空间上限2.5. 结果验证一、预先准备 1. 现象 场景1&#xff1a; 昨天尝试通过plsqldev尝试连接oracle数据库&#xff0c;报错&#xff…

qt中根据数据解析的结果动态的创建控件并布局

引言 有时候程序中的控件的个数是无法预测的&#xff0c;当程序启动时&#xff0c;根据解析的数据动态的创建n行m列的控件&#xff0c;并为其布局。下面记录一下动态创建控件&#xff0c;并布局。 运行效果 示例 此示例主要是从json配置文件中读取参数&#xff0c;然后根据参…

5分钟带你了解Kafka的技术架构 | 技术头条

戳蓝字“CSDN云计算”关注我们哦&#xff01;技术头条&#xff1a;干货、简洁、多维全面。更多云计算精华知识尽在眼前&#xff0c;get要点、solve难题&#xff0c;统统不在话下&#xff01;大家都知道 Kafka 是一个非常牛逼的消息队列框架&#xff0c;阿里的 RocketMQ 也是在 …

Mysql和Oracle 数据库操作工具类

适配Mysql和Oracle数据源 文章目录1. 适配Mysql和Oracle数据源2. 适配于Mysql数据源3. 适配Oeacle数据源1. 适配Mysql和Oracle数据源 package cn.stylefeng.guns.generator.core.util; import cn.stylefeng.guns.generator.modular.entity.DatabaseInfo; import lombok.extern…

QString类型转换为bool类型

方法 直接上代码 QString str "12.345";QVariant onLineTemp str;bool isValue onLineTemp.toBool();

Spark精华问答 | Spark的计算方法是什么?

戳蓝字“CSDN云计算”关注我们哦&#xff01;Spark是一个针对超大数据集合的低延迟的集群分布式计算系统&#xff0c;比MapReducer快40倍左右&#xff0c;是hadoop的升级版本&#xff0c;Hadoop作为第一代产品使用HDFS&#xff0c;第二代加入了Cache来保存中间计算结果&#xf…

qt中创建控件布局以及删除原有布局和控件

引言 当根据数据来创建控件并布局时&#xff0c;如果数据更新&#xff0c;那么之前创建的控件便需要删除后重新创建布局。该文主要说明将原来的布局和控件删除&#xff0c;重新创建并布局。 示例 先看一下ui文件&#xff1a; 下面是实现代码&#xff1a; void StatusViewDi…

Spring精华问答 | Spring 能帮我们做什么?

Spring框架是一个开源的Java平台,它提供了非常容易,非常迅速地开发健壮的Java应用程序的全面的基础设施支持。今天就让我们一起来看看关于Spring的精华问答吧。1Q&#xff1a;什么是Spring框架&#xff1f;A&#xff1a;Spring框架是一个为Java应用程序的开发提供了综合、广泛的…

qt自定义控件的样式

引言 自定义控件创建后&#xff0c;有时需要设置样式&#xff0c;下面记录一下设置样式时需要注意的点。 注意 1.设置自定义控件的样式时&#xff0c;需要下面的代码&#xff1a; void paintEvent(QPaintEvent *event) {Q_UNUSED(event);QStyleOption opt;opt.init(this);Q…

linux 上传文件 rz命令 提示command not found 解决方法

-bash: rz: command not found rz命令没找到&#xff1f; 执行sz&#xff0c;同样也没找到。 安装lrzsz&#xff1a; yum -y install lrzsz现在就可以正常使用rz、sz命令上传、下载数据了。 使用方法&#xff1a; 上传文件 rz filename下载文件 sz filename

IoT与大数据 如何激发数字营销最大潜能?

戳蓝字“CSDN云计算”关注我们哦&#xff01;技术头条&#xff1a;干货、简洁、多维全面。更多云计算精华知识尽在眼前&#xff0c;get要点、solve难题&#xff0c;统统不在话下&#xff01;译者&#xff1a;风车云马 物联网与大数据概述物联网(IOT)简单理解&#xff0c;除了电…