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:
生成的文件的內容:
<?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文件的生成,理解其意便可。
總結
- 上一篇: java铃声类_java多线程抓取铃声多
- 下一篇: oracle11管理员连接数据库,1.运