QTXML節(jié)點(diǎn)創(chuàng)建添加刪除_第1頁
QTXML節(jié)點(diǎn)創(chuàng)建添加刪除_第2頁
QTXML節(jié)點(diǎn)創(chuàng)建添加刪除_第3頁
QTXML節(jié)點(diǎn)創(chuàng)建添加刪除_第4頁
QTXML節(jié)點(diǎn)創(chuàng)建添加刪除_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、 QT中如何實(shí)現(xiàn)XML文件的創(chuàng)建、增加、修改、刪除功能。分類: QT學(xué)習(xí)2013-03-21 16:01 6285人閱讀 評(píng)論(0) 收藏 舉報(bào)一、首先咱們得認(rèn)識(shí)處理XML的方法有那些,QT提供了那些函數(shù)用于處理。                      我們知道對(duì)XML的操作有兩種方法,即DOM方式和SAX方式。二

2、者主要區(qū)別是:DOM實(shí)現(xiàn)方式操作非常簡(jiǎn)單,但不適合處理過大文件;而SAX實(shí)現(xiàn)方式是能處理很大的XML文件,但是需要開發(fā)者寫一些復(fù)雜的代碼。Qt提供了對(duì)應(yīng)于這兩種用于讀取、操作和編寫XML的實(shí)現(xiàn)類,分別是QDomDocument類和QXmlStreamReader類,由于在項(xiàng)目中涉及的文件不大,因此我們選用QDomDocument類來處理。 二、咱們需要了解XML文件的最常見格式:          1、需要知道一個(gè)XML文件有且只有一個(gè)根節(jié)點(diǎn);   

3、60;     2、子節(jié)點(diǎn)與元素的概念,以及它們之間的聯(lián)系(如何包含);  三、對(duì)XML文件創(chuàng)建,讀取、修改:  1. 創(chuàng)建節(jié)點(diǎn),將其寫入XML文件,主要操作包括:  QDomDocument doc;1).創(chuàng)建根節(jié)點(diǎn):QDomElement root = doc.createElement("root")2).創(chuàng)建元素節(jié)點(diǎn):QDomElement element = doc.createElement("nodeName");3).添加元素節(jié)點(diǎn)到根節(jié)點(diǎn):

4、root. appendChild(element);4).創(chuàng)建元素文本:QDomText nodeText=doc.createTextNode("text");5).添加元素文本到元素節(jié)點(diǎn):element. appendChild(nodeText); 在本項(xiàng)目中,假設(shè)便簽的屬性有序號(hào)、名字、內(nèi)容、字體、字號(hào)、顏色、粗細(xì)、斜體、下劃線這幾項(xiàng),則在文件中添加一個(gè)便簽節(jié)點(diǎn)的操作如下:QDomDocument doc;instruction = doc.createProcessingInstruction("xml","version

5、="1.0" encoding="UTF-8"");doc.appendChild(instruction);QDomElement root = doc.createElement_x_x("Notes");doc.appendChild(root);QDomElement note = doc.createElement_x_x("note");root.appendChild(note);QDomElement no = doc.createElement_x_x("no");n

6、ote.appendChild(no);.QDomText no_text = doc.createTextNode("001");.則得到一個(gè)便簽節(jié)點(diǎn),將其保存到test.xml文件中,代碼如下:QFile file("test.xml");if (!file.open(QIODevice:WriteOnly | QIODevice:Truncate |QIODevice:Text)return ;QTextStream out(&file);out.setCodec("UTF-8");doc.save(out,4,QDom

7、Node:EncodingFromTextStream);file.close();則test.xml文件:<?xml version="1.0" encoding="UTF-8"?><Notes><note><no>001</no><name>2010-05-10(13:53:24)</name><content>A meeting!</content><font>Script MT Bold</font><font

8、Size>16</fontSize><color> #00ffff</color><weight>0</weight><fontItalic>true</fontItalic><fontUnderline>true</fontUnderline></note></Notes>上面是創(chuàng)建一個(gè)便簽節(jié)點(diǎn),若要繼續(xù)添加便簽節(jié)點(diǎn),則需要在已有節(jié)點(diǎn)后增加一個(gè)新節(jié)點(diǎn),并重寫入XML文件。 2. 加載、查找便簽時(shí)要讀取XML文檔中的節(jié)點(diǎn)信息,DOM實(shí)現(xiàn)方式是將整個(gè)

9、文檔當(dāng)作一個(gè)對(duì)象來裝入內(nèi)存進(jìn)行處理,然后開發(fā)者可以訪問這個(gè)對(duì)象中的每一個(gè)節(jié)點(diǎn),每一個(gè)節(jié)點(diǎn)對(duì)應(yīng)XML文件里的一個(gè)標(biāo)記。 主要操作包括:1).讀取根節(jié)點(diǎn):QDomElement root = doc.documentElement();2).讀取第一個(gè)子節(jié)點(diǎn):QDomNode node = root.firstChild();3).讀取下一個(gè)子節(jié)點(diǎn):node = node.nextSibling();4).匹配結(jié)點(diǎn)標(biāo)記:node.toElement().tagName() = "note"5).讀取節(jié)點(diǎn)文本:no = childNode.toText().data()

10、;/=遍歷節(jié)點(diǎn)=可以通過doc.childNodes()獲得doc的所有的子節(jié)點(diǎn)列表QDomNodeList。比如QDomNodeList list=doc.childNodes();for(int i=0;i<list.count();i+)QDomNode node=list.at(i);/好的風(fēng)格當(dāng)然是把定義寫在外面:(/qDebug()<<”node name is “<<node.nodeName();/qDebug()<<”node type is “<<.nodeType();/=以下是項(xiàng)目中讀取便簽屬性的函數(shù)實(shí)現(xiàn)代碼:voi

11、d MainWindow:parseAttr(const QDomElement &element)QString no,name,content,font,fontSize,color;QDomNode node = element.firstChild();while (!node.isNull() if (node.toElement().tagName() = "note") /匹配note節(jié)點(diǎn)parseAttr(node.toElement(); else if (node.toElement().tagName() = "no") /

12、匹配屬性noQDomNode childNode = node.firstChild();if (childNode.nodeType() = QDomNode:TextNode) no = childNode.toText().data();else if (node.toElement().tagName() = "name") /匹配屬性name.node = node.nextSibling();/讀取兄弟節(jié)點(diǎn)3. 刪除便簽時(shí),要?jiǎng)h除相應(yīng)的XML節(jié)點(diǎn),用到的主要函數(shù)為:root.removeChild(node); 但在刪除某個(gè)節(jié)點(diǎn)后要重寫整個(gè)文件。 &#

13、160;          以上對(duì)XML文件的重寫操作是必須的,因此在文件的打開方式中要加上QIODevice:Truncate,表示覆蓋重寫。目前還沒有找到可以直接修改文件的方法,但若文件很大的情況下,必須考慮相應(yīng)的效率問題。  4.讀取XML文件內(nèi)容:/=打開document=add me = QString xmlPath = "filename.xml" QFile file(xmlPath); if (!file.open(Q

14、File:ReadOnly | QFile:Text)  return ; QString errorStr; int errorLine; int errorColumn; QDomDocument doc; if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn)  return ; file.close();/也可以用doc.setContent(&file)帶一個(gè)參

15、數(shù)就行。/=然后按照上面方式遍歷數(shù)據(jù),修改后在以上面相同的方式寫入到XML文件中。/=附加一段代碼創(chuàng)建XML文件=  #include <QApplication>#include <QFile>#include <QDomDocument>#include <QTextStream>#include <QTextCodec>#include <QMessageBox>int main (int argc,char *argv)     QApplication

16、a (argc,argv);     QFile file("InitInfo.xml");  if(!file.open(QFile:ReadOnly | QFile:Text)       / QMessageBox:information(this,tr("error"),tr("無法打開XML文件");      return 1;    /*第一步

17、,將file中的內(nèi)容讀入doc內(nèi)存中*/     QDomDocument doc;   doc.setContent(&file,false,&errorStr,&errorLine,&errorColumn);   file.close(); /*第二步,創(chuàng)建根節(jié)點(diǎn)*/   QDomElement root = doc.createElement("root");     /創(chuàng)建根節(jié)點(diǎn)

18、60; /*第三步:創(chuàng)建子節(jié)點(diǎn)和元素,并將元素加入相應(yīng)節(jié)點(diǎn)內(nèi),最后將節(jié)點(diǎn)加入根節(jié)點(diǎn)中*/  QDomElement element = doc.createElement("device3");     /創(chuàng)建第一個(gè)子節(jié)點(diǎn)  element.setAttribute("ChildName","Content");  QDomElement el_address = doc.createElement("Content"); 

19、/創(chuàng)建一個(gè)元素 該元素有三種屬性  el_address.setAttribute("key","equipmentname");  el_address.setAttribute("value","CSC101B");  el_address.setAttribute("name","itemName");  element.appendChild(el_address);     &

20、#160;                 /將該元素增加到第一節(jié)點(diǎn)中.  QDomElement el_path = doc.createElement("path");       /創(chuàng)建第二個(gè)元素,  QDomText text_path = doc.createTextNode("aaa"); 

21、60;       /創(chuàng)建該元素的文本  el_path.appendChild(text_path);                        /將該元素文本增加到該元素中  element.appendChild(el_path);    

22、                     /將該元素添加到第一個(gè)子節(jié)點(diǎn)中  root.appendChild(element);                      

23、;     /將該子節(jié)點(diǎn)加入到根節(jié)點(diǎn)下. QFile f("InitInfo.xml"); if(!f.open(QFile:WriteOnly | QFile:Text)  return 1;  QTextStream out(&f);  QTextCodec * codec = QTextCodec:codecForName("gbk");        /此處是設(shè)置文件支持

24、的編碼格式  out.setCodec(codec);  QString strHead("version =1.0 encoding =GB2312" );  doc.appendChild(doc.createProcessingInstruction("xml" ,strHead );  out << doc.toString();  root.save(out,2);  f.close();  return 0; 其它內(nèi)容:1 首先對(duì)XML文件進(jìn)行操作,打

25、開文件。這個(gè)其實(shí)就是對(duì)文件進(jìn)行操作,可以把它直接定義為構(gòu)造函數(shù),在對(duì)對(duì)象進(jìn)行初始化時(shí)完成。TopoDataReader:TopoDataReader(const wstring &filePath):_filePath(filePath),_qDomDoc("mydocument"),_qFile(QString:fromStdWString(filePath) if (!_qFile.open(QIODevice:ReadOnly)   return;  if (!_qDomDoc.setContent(

26、&_qFile)   _qFile.close();  return; 2 讀取XML文件中的節(jié)點(diǎn)內(nèi)容假設(shè)XML文件格式如下(1)<switchs><switch snmpip=211.87.235.136 newwork=front></switch></switchs>(2)<ip>211.87.235.136</ip>對(duì)于第一種情況,采用如下方法:QDomElement docElem = _qDomDoc.documentElement();QDo

27、mNode nodeswitch=docElem.elementsByTagName("switch ");/紅色的為標(biāo)簽名QDomElement elemnodeswitch=nodeswitch.toElement();string snmpip=qPrintable(elemnodeswitch.attribute("snmpip");/network的也是如此方法獲取對(duì)于第二種情況,采用如下方法:直接調(diào)用text() API就可以了string ip=qPrintable(elementnodeip.text();但是,假設(shè)文件中有多個(gè)同樣的節(jié)點(diǎn)

28、,如下<a><b></b><b></b></a>這樣用elementsByTagName("b")返回的就是一個(gè)childNodes()而不是一個(gè)單獨(dú)的node了。我們可以使用at()方法具體定位。另外,我們還可以使用這樣一種方法獲取節(jié)點(diǎn)的值,假設(shè)XML文件如下- +<switch snmpIp="192.168.120.251" network="front"><name>前端主交換機(jī)</name><description />-

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論