qt创建xml文件

引言

由于项目需要,现学现卖,先将下面两个例子来记录一下。

示例1

直接上代码,创建xml文件。

void Config::createServerInfoFile()
{QString strPath = "路径";//这里的路径是将要创建的xml的完整路径,包含文件名xxx.xmlQFile file(strPath);if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {//之前的内容被清空qDebug()<<QStringLiteral("打开")<<strPath<<QStringLiteral("文件失败!");return;}QDomDocument doc;QDomProcessingInstruction processInstruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");doc.appendChild(processInstruction);QDomElement root = doc.createElement("server");doc.appendChild(root);QDomElement server1 = doc.createElement("helperIp");QDomText server1Text = doc.createTextNode("127.0.0.1");server1.appendChild(server1Text);root.appendChild(server1);QDomElement port1 = doc.createElement("sendPort");QDomText port1Text = doc.createTextNode("9999");port1.appendChild(port1Text);root.appendChild(port1);QDomElement port11 = doc.createElement("receviePort");QDomText port11Text = doc.createTextNode("9000");port11.appendChild(port11Text);root.appendChild(port11);QDomElement server2 = doc.createElement("kserver");QDomText server2Text = doc.createTextNode("192.168.1.5");server2.appendChild(server2Text);root.appendChild(server2);QDomElement port2 = doc.createElement("kPort");QDomText port2Text = doc.createTextNode("9080");port2.appendChild(port2Text);root.appendChild(port2);QDomElement upScreenServer = doc.createElement("upServer");QDomText upScreenServerText = doc.createTextNode("192.168.1.2");upScreenServer.appendChild(upScreenServerText);root.appendChild(upScreenServer);QDomElement upScreenPort = doc.createElement("upPort");QDomText upScreenPortText = doc.createTextNode("7999");upScreenPort.appendChild(upScreenPortText);root.appendChild(upScreenPort);QTextStream outFile(&file);doc.save(outFile,4);//缩进4格file.close();
}

创建的xml文件的内容如下:

<?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>

下面是示例2,两个示例是一样的,若看完示例1已经明白大致怎么创建,此时就不需要看示例二了。
示例2:

const QString strKvmConfigFileName = "/kvmSeatSources.xml";QString Config::getExeFilePath()
{QString strPath = QCoreApplication::applicationDirPath();return strPath;
}void Config::createKvmSeatSourceFile()
{QString strPath = getExeFilePath();strPath += strKvmConfigFileName;QFile file(strPath);if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {//之前的内容被清空qDebug()<<QStringLiteral("打开")<<strPath<<QStringLiteral("文件失败!");return;}QDomDocument doc;QDomProcessingInstruction processInstruct = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");doc.appendChild(processInstruct);QDomElement root = doc.createElement("kvm");doc.appendChild(root);//创建第一个子节点QDomElement firstElement = doc.createElement("seatInfo");root.appendChild(firstElement);for (int i = 0; i < 12; ++i) {QDomElement seatElement = doc.createElement("seat");firstElement.appendChild(seatElement);QDomElement sidElement = doc.createElement("sid");QDomText sidText = doc.createTextNode("12");sidElement.appendChild(sidText);seatElement.appendChild(sidElement);QDomElement seatNameElement = doc.createElement("seatName");QDomText seatNameText = doc.createTextNode("admin1");seatNameElement.appendChild(seatNameText);seatElement.appendChild(seatNameElement);QDomElement seatIdElement = doc.createElement("seatId");QDomText seatIdText = doc.createTextNode("12289832899desa");seatIdElement.appendChild(seatIdText);seatElement.appendChild(seatIdElement);QDomElement seatIpElement = doc.createElement("seatIp");QDomText seatIpText = doc.createTextNode("192.168.1.20");seatIpElement.appendChild(seatIpText);seatElement.appendChild(seatIpElement);}//创建第二个子节点QDomElement secondElement = doc.createElement("signalsource");root.appendChild(secondElement);for (int i = 0; i < 12; ++i) {QDomElement sourceElement = doc.createElement("source");secondElement.appendChild(sourceElement);QDomElement uidElement = doc.createElement("uid");QDomText uidText = doc.createTextNode(QString::number(i+1));uidElement.appendChild(uidText);sourceElement.appendChild(uidElement);QDomElement ipElement = doc.createElement("ip");QDomText ipText = doc.createTextNode("192.168.1.2");ipElement.appendChild(ipText);sourceElement.appendChild(ipElement);}QTextStream outStream(&file);doc.save(outStream,4);file.close();
}

生成的文件的内容:

<?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文件,可能文件的某些字段的值对不上,是因为后期手动修改了,不影响xml文件的生成,理解其意便可。

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

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

相关文章

Guns 删除功能_入门试炼07

一、删除功能默认实现 选中需要删除的数据执行删除操作即可 删除前截图&#xff1a; 删除后截图&#xff1a; 从截图中可以看出&#xff0c;删除的那条数据已经不在了。 想学习更多微服务、分布式、中间件、数据库、项目快速构建等系列技术 请跳转Gblfy个人专栏: www.gblf…

qt中xml的读取

引言 该文章是基于上一篇文章&#xff0c;xml生成而写的&#xff0c;其所用的xml文件的内容是上一篇中的内容。 示例1 文件的内容&#xff1a; <?xml version"1.0" encoding"UTF-8"?> <server><helperIp>127.0.0.1</helperIp&g…

第一次更名为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…