项目配置
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 }
写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>