轉自:https://blog.csdn.net/hpu11/article/details/80227093
項目配置
pro文件里面添加QT+=xml
include <QtXml>,也可以include <QDomDocument>
項目文件:
.pro 文件
1 QT += core xml 2 3 QT -= gui 4 5 TARGET = xmltest 6 CONFIG += console 7 CONFIG -= app_bundle 8 9 TEMPLATE = app 10 11 12 SOURCES += main.cpp
主程序:
main.cpp
1 #include <QCoreApplication> 2 #include <QtXml> //也可以include <QDomDocument> 3 4 //寫xml 5 void WriteXml() 6 { 7 //打開或創建文件 8 QFile file("test.xml"); //相對路徑、絕對路徑、資源路徑都可以 9 if(!file.open(QFile::WriteOnly|QFile::Truncate)) //可以用QIODevice,Truncate表示清空原來的內容 10 return; 11 12 QDomDocument doc; 13 //寫入xml頭部 14 QDomProcessingInstruction instruction; //添加處理命令 15 instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\""); 16 doc.appendChild(instruction); 17 //添加根節點 18 QDomElement root=doc.createElement("library"); 19 doc.appendChild(root); 20 //添加第一個子節點及其子元素 21 QDomElement book=doc.createElement("book"); 22 book.setAttribute("id",1); //方式一:創建屬性 其中鍵值對的值可以是各種類型 23 QDomAttr time=doc.createAttribute("time"); //方式二:創建屬性 值必須是字符串 24 time.setValue("2013/6/13"); 25 book.setAttributeNode(time); 26 QDomElement title=doc.createElement("title"); //創建子元素 27 QDomText text; //設置括號標簽中間的值 28 text=doc.createTextNode("C++ primer"); 29 book.appendChild(title); 30 title.appendChild(text); 31 QDomElement author=doc.createElement("author"); //創建子元素 32 text=doc.createTextNode("Stanley Lippman"); 33 author.appendChild(text); 34 book.appendChild(author); 35 root.appendChild(book); 36 37 //添加第二個子節點及其子元素,部分變量只需重新賦值 38 book=doc.createElement("book"); 39 book.setAttribute("id",2); 40 time=doc.createAttribute("time"); 41 time.setValue("2007/5/25"); 42 book.setAttributeNode(time); 43 title=doc.createElement("title"); 44 text=doc.createTextNode("Thinking in Java"); 45 book.appendChild(title); 46 title.appendChild(text); 47 author=doc.createElement("author"); 48 text=doc.createTextNode("Bruce Eckel"); 49 author.appendChild(text); 50 book.appendChild(author); 51 root.appendChild(book); 52 53 //輸出到文件 54 QTextStream out_stream(&file); 55 doc.save(out_stream,4); //縮進4格 56 file.close(); 57 58 } 59 60 //讀xml 61 void ReadXml() 62 { 63 //打開或創建文件 64 QFile file("test.xml"); //相對路徑、絕對路徑、資源路徑都行 65 if(!file.open(QFile::ReadOnly)) 66 return; 67 68 QDomDocument doc; 69 if(!doc.setContent(&file)) 70 { 71 file.close(); 72 return; 73 } 74 file.close(); 75 76 QDomElement root=doc.documentElement(); //返回根節點 77 qDebug()<<root.nodeName(); 78 QDomNode node=root.firstChild(); //獲得第一個子節點 79 while(!node.isNull()) //如果節點不空 80 { 81 if(node.isElement()) //如果節點是元素 82 { 83 QDomElement e=node.toElement(); //轉換為元素,注意元素和節點是兩個數據結構,其實差不多 84 qDebug()<<e.tagName()<<" "<<e.attribute("id")<<" "<<e.attribute("time"); //打印鍵值對,tagName和nodeName是一個東西 85 QDomNodeList list=e.childNodes(); 86 for(int i=0;i<list.count();i++) //遍歷子元素,count和size都可以用,可用於標簽數計數 87 { 88 QDomNode n=list.at(i); 89 if(node.isElement()) 90 qDebug()<<n.nodeName()<<":"<<n.toElement().text(); 91 } 92 } 93 node=node.nextSibling(); //下一個兄弟節點,nextSiblingElement()是下一個兄弟元素,都差不多 94 } 95 96 } 97 98 //增加xml內容 99 void AddXml() 100 { 101 //打開文件 102 QFile file("test.xml"); //相對路徑、絕對路徑、資源路徑都可以 103 if(!file.open(QFile::ReadOnly)) 104 return; 105 106 //增加一個一級子節點以及元素 107 QDomDocument doc; 108 if(!doc.setContent(&file)) 109 { 110 file.close(); 111 return; 112 } 113 file.close(); 114 115 QDomElement root=doc.documentElement(); 116 QDomElement book=doc.createElement("book"); 117 book.setAttribute("id",3); 118 book.setAttribute("time","1813/1/27"); 119 QDomElement title=doc.createElement("title"); 120 QDomText text; 121 text=doc.createTextNode("Pride and Prejudice"); 122 title.appendChild(text); 123 book.appendChild(title); 124 QDomElement author=doc.createElement("author"); 125 text=doc.createTextNode("Jane Austen"); 126 author.appendChild(text); 127 book.appendChild(author); 128 root.appendChild(book); 129 130 if(!file.open(QFile::WriteOnly|QFile::Truncate)) //先讀進來,再重寫,如果不用truncate就是在后面追加內容,就無效了 131 return; 132 //輸出到文件 133 QTextStream out_stream(&file); 134 doc.save(out_stream,4); //縮進4格 135 file.close(); 136 } 137 138 //刪減xml內容 139 void RemoveXml() 140 { 141 //打開文件 142 QFile file("test.xml"); //相對路徑、絕對路徑、資源路徑都可以 143 if(!file.open(QFile::ReadOnly)) 144 return; 145 146 //刪除一個一級子節點及其元素,外層節點刪除內層節點於此相同 147 QDomDocument doc; 148 if(!doc.setContent(&file)) 149 { 150 file.close(); 151 return; 152 } 153 file.close(); //一定要記得關掉啊,不然無法完成操作 154 155 QDomElement root=doc.documentElement(); 156 QDomNodeList list=doc.elementsByTagName("book"); //由標簽名定位 157 for(int i=0;i<list.count();i++) 158 { 159 QDomElement e=list.at(i).toElement(); 160 if(e.attribute("time")=="2007/5/25") //以屬性名定位,類似於hash的方式,warning:這里僅僅刪除一個節點,其實可以加個break 161 root.removeChild(list.at(i)); 162 } 163 164 if(!file.open(QFile::WriteOnly|QFile::Truncate)) 165 return; 166 //輸出到文件 167 QTextStream out_stream(&file); 168 doc.save(out_stream,4); //縮進4格 169 file.close(); 170 } 171 172 //更新xml內容 173 void UpdateXml() 174 { 175 //打開文件 176 QFile file("test.xml"); //相對路徑、絕對路徑、資源路徑都可以 177 if(!file.open(QFile::ReadOnly)) 178 return; 179 180 //更新一個標簽項,如果知道xml的結構,直接定位到那個標簽上定點更新 181 //或者用遍歷的方法去匹配tagname或者attribut,value來更新 182 QDomDocument doc; 183 if(!doc.setContent(&file)) 184 { 185 file.close(); 186 return; 187 } 188 file.close(); 189 190 QDomElement root=doc.documentElement(); 191 QDomNodeList list=root.elementsByTagName("book"); 192 QDomNode node=list.at(list.size()-1).firstChild(); //定位到第三個一級子節點的子元素 193 QDomNode oldnode=node.firstChild(); //標簽之間的內容作為節點的子節點出現,當前是Pride and Projudice 194 node.firstChild().setNodeValue("Emma"); 195 QDomNode newnode=node.firstChild(); 196 node.replaceChild(newnode,oldnode); 197 198 if(!file.open(QFile::WriteOnly|QFile::Truncate)) 199 return; 200 //輸出到文件 201 QTextStream out_stream(&file); 202 doc.save(out_stream,4); //縮進4格 203 file.close(); 204 } 205 206 int main(int argc, char *argv[]) 207 { 208 209 qDebug()<<"write xml to file..."; 210 WriteXml(); 211 qDebug()<<"read xml to display..."; 212 ReadXml(); 213 qDebug()<<"add contents to xml..."; 214 AddXml(); 215 qDebug()<<"remove contents from xml..."; 216 RemoveXml(); 217 qDebug()<<"update contents to xml..."; 218 UpdateXml(); 219 return 0; 220 221 } 222
寫xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <library> 3 <book id="1" time="2013/6/13"> 4 <title>C++ primer</title> 5 <author>Stanley Lippman</author> 6 </book> 7 <book id="2" time="2007/5/25"> 8 <title>Thinking in Java</title> 9 <author>Bruce Eckel</author> 10 </book> 11 </library>
增加xml
1 <?xml version='1.0' encoding='UTF-8'?> 2 <library> 3 <book time="2013/6/13" id="1"> 4 <title>C++ primer</title> 5 <author>Stanley Lippman</author> 6 </book> 7 <book time="2007/5/25" id="2"> 8 <title>Thinking in Java</title> 9 <author>Bruce Eckel</author> 10 </book> 11 <book time="1813/1/27" id="3"> 12 <title>Pride and Prejudice</title> 13 <author>Jane Austen</author> 14 </book> 15 </library>
刪除xml
1 <?xml version='1.0' encoding='UTF-8'?> 2 <library> 3 <book time="2013/6/13" id="1"> 4 <title>C++ primer</title> 5 <author>Stanley Lippman</author> 6 </book> 7 <book time="2007/5/25" id="2"> 8 <title>Thinking in Java</title> 9 <author>Bruce Eckel</author> 10 </book> 11 <book time="1813/1/27" id="3"> 12 <title>Pride and Prejudice</title> 13 <author>Jane Austen</author> 14 </book> 15 </library>
更新xml
1 <?xml version='1.0' encoding='UTF-8'?> 2 <library> 3 <book id="1" time="2013/6/13"> 4 <title>C++ primer</title> 5 <author>Stanley Lippman</author> 6 </book> 7 <book id="3" time="1813/1/27"> 8 <title>Emma</title> 9 <author>Jane Austen</author> 10 </book> 11 </library>