Qt5对xml文件常用的操作(读写,增删改查)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Qt5对xml⽂件常⽤的操作(读写,增删改查)转⾃:https:///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
5void WriteXml()
6 {
7//打开或创建⽂件
8 QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
9if(!file.open(QFile::WriteOnly|QFile::Truncate)) //可以⽤QIODevice,Truncate表⽰清空原来的内容
10return;
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
61void ReadXml()
62 {
63//打开或创建⽂件
64 QFile file("test.xml"); //相对路径、绝对路径、资源路径都⾏
65if(!file.open(QFile::ReadOnly))
66return;
67
68 QDomDocument doc;
69if(!doc.setContent(&file))
70 {
71 file.close();
72return;
73 }
74 file.close();
75
76 QDomElement root=doc.documentElement(); //返回根节点
77 qDebug()<<root.nodeName();
78 QDomNode node=root.firstChild(); //获得第⼀个⼦节点
79while(!node.isNull()) //如果节点不空
80 {
81if(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();
86for(int i=0;i<list.count();i++) //遍历⼦元素,count和size都可以⽤,可⽤于标签数计数
87 {
88 QDomNode n=list.at(i);
89if(node.isElement())
90 qDebug()<<n.nodeName()<<":"<<n.toElement().text();
91 }
92 }
93 node=node.nextSibling(); //下⼀个兄弟节点,nextSiblingElement()是下⼀个兄弟元素,都差不多
94 }
95
96 }
97
98//增加xml内容
99void AddXml()
100 {
101//打开⽂件
102 QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
103if(!file.open(QFile::ReadOnly))
104return;
105
106//增加⼀个⼀级⼦节点以及元素
107 QDomDocument doc;
108if(!doc.setContent(&file))
109 {
110 file.close();
111return;
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
130if(!file.open(QFile::WriteOnly|QFile::Truncate)) //先读进来,再重写,如果不⽤truncate就是在后⾯追加内容,就⽆效了
131return;
132//输出到⽂件
133 QTextStream out_stream(&file);
134 doc.save(out_stream,4); //缩进4格
138//删减xml内容
139void RemoveXml()
140 {
141//打开⽂件
142 QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
143if(!file.open(QFile::ReadOnly))
144return;
145
146//删除⼀个⼀级⼦节点及其元素,外层节点删除内层节点于此相同
147 QDomDocument doc;
148if(!doc.setContent(&file))
149 {
150 file.close();
151return;
152 }
153 file.close(); //⼀定要记得关掉啊,不然⽆法完成操作
154
155 QDomElement root=doc.documentElement();
156 QDomNodeList list=doc.elementsByTagName("book"); //由标签名定位
157for(int i=0;i<list.count();i++)
158 {
159 QDomElement e=list.at(i).toElement();
160if(e.attribute("time")=="2007/5/25") //以属性名定位,类似于hash的⽅式,warning:这⾥仅仅删除⼀个节点,其实可以加个break 161 root.removeChild(list.at(i));
162 }
163
164if(!file.open(QFile::WriteOnly|QFile::Truncate))
165return;
166//输出到⽂件
167 QTextStream out_stream(&file);
168 doc.save(out_stream,4); //缩进4格
169 file.close();
170 }
171
172//更新xml内容
173void UpdateXml()
174 {
175//打开⽂件
176 QFile file("test.xml"); //相对路径、绝对路径、资源路径都可以
177if(!file.open(QFile::ReadOnly))
178return;
179
180//更新⼀个标签项,如果知道xml的结构,直接定位到那个标签上定点更新
181//或者⽤遍历的⽅法去匹配tagname或者attribut,value来更新
182 QDomDocument doc;
183if(!doc.setContent(&file))
184 {
185 file.close();
186return;
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
198if(!file.open(QFile::WriteOnly|QFile::Truncate))
199return;
200//输出到⽂件
201 QTextStream out_stream(&file);
202 doc.save(out_stream,4); //缩进4格
203 file.close();
204 }
205
206int 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();
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>。