最近研究了一下qt下对xml文件的读写,小计一下,成为自己的知识。 main函数调用:
1 #include <QApplication>
2 #include "readconfig.h"
3 #include "writeconfig.h"
4
5 int main(int argc,char **argv)
6 {
7 QApplication a(argc,argv);
8
9 //ReadConfig readConfig;
10
11 //readConfig.changeSave();
12
13 WriteConfig writeConfig;
14
15 QFile file;
16 QString fileName = "Motor.xml";
17 if(!file.exists(fileName))
18 {
19 writeConfig.createFile(fileName);
20 }
21
22 writeConfig.writeFile(fileName);
23
24 a.exec();
25
26 return 0;
27 }
写入xml文件WriteConfig.cpp
1 #include "writeconfig.h"
2
3 WriteConfig::WriteConfig()
4 {
5 }
6
7
8 bool WriteConfig::writeFile(QString fileName)
9 {
10 QFile file(fileName);
11
12 if(!file.open(QFile::ReadOnly | QFile::Text))
13 {
14 qDebug()<<"open for add error...";
15 return false;
16 }
17
18 QDomDocument doc;
19
20 QString errorStr;
21 int errorLine;
22 int errorColumn;
23
24 if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
25 {
26 qDebug()<<"add setcontent error..." <<"errorStr&&&&"<<errorStr<<"errorLine"<<errorLine<<"errorColume"<<errorColumn;
27 //file.close();
28 }
29
30
31 file.close();
32
33 QDomElement root = doc.documentElement();
34 if(root.isNull())
35 {
36 root = doc.createElement("configinfo");
37 }
38 QDomElement elementRoot = doc.createElement("motor");
39 elementRoot.setAttribute("mount","6");
40 QDomElement elementSpeed = doc.createElement("speed");
41 QDomElement elementAcceleSpeed= doc.createElement("accelespeed");
42 QDomElement elementAASpeed = doc.createElement("aaspeed");
43 QDomElement elementPosition = doc.createElement("position");
44
45 QDomElement elementV1 = doc.createElement("v");
46 QDomElement elementV2 = doc.createElement("v");
47 QDomElement elementV3 = doc.createElement("v");
48 QDomElement elementV4 = doc.createElement("v");
49 QDomElement elementV5 = doc.createElement("v");
50 QDomElement elementV6 = doc.createElement("v");
51 QDomElement elementA1 = doc.createElement("a");
52 QDomElement elementA2 = doc.createElement("a");
53 QDomElement elementA3 = doc.createElement("a");
54 QDomElement elementA4 = doc.createElement("a");
55 QDomElement elementA5 = doc.createElement("a");
56 QDomElement elementA6 = doc.createElement("a");
57 QDomElement elementAA1 = doc.createElement("aa");
58 QDomElement elementAA2 = doc.createElement("aa");
59 QDomElement elementAA3 = doc.createElement("aa");
60 QDomElement elementAA4 = doc.createElement("aa");
61 QDomElement elementAA5 = doc.createElement("aa");
62 QDomElement elementAA6 = doc.createElement("aa");
63 QDomElement elementP1 = doc.createElement("p");
64 QDomElement elementP2 = doc.createElement("p");
65 QDomElement elementP3 = doc.createElement("p");
66 QDomElement elementP4 = doc.createElement("p");
67 QDomElement elementP5 = doc.createElement("p");
68 QDomElement elementP6 = doc.createElement("p");
69
70
71 QDomText text;
72 text = doc.createTextNode("123.25");
73 elementV1.appendChild(text);
74 elementSpeed.appendChild(elementV1);
75 text = doc.createTextNode("223.25");
76 elementV2.appendChild(text);
77 elementSpeed.appendChild(elementV2);
78 text = doc.createTextNode("323.25");
79 elementV3.appendChild(text);
80 elementSpeed.appendChild(elementV3);
81 text = doc.createTextNode("423.25");
82 elementV4.appendChild(text);
83 elementSpeed.appendChild(elementV4);
84 text = doc.createTextNode("523.25");
85 elementV5.appendChild(text);
86 elementSpeed.appendChild(elementV5);
87 text = doc.createTextNode("623.25");
88 elementV6.appendChild(text);
89 elementSpeed.appendChild(elementV6);
90
91 text = doc.createTextNode("1023.25");
92 elementA1.appendChild(text);
93 elementAcceleSpeed.appendChild(elementA1);
94 text = doc.createTextNode("2023.25");
95 elementA2.appendChild(text);
96 elementAcceleSpeed.appendChild(elementA2);
97 text = doc.createTextNode("3023.25");
98 elementA3.appendChild(text);
99 elementAcceleSpeed.appendChild(elementA3);
100 text = doc.createTextNode("4023.25");
101 elementA4.appendChild(text);
102 elementAcceleSpeed.appendChild(elementA4);
103 text = doc.createTextNode("5023.25");
104 elementA5.appendChild(text);
105 elementAcceleSpeed.appendChild(elementA5);
106 text = doc.createTextNode("6023.25");
107 elementA6.appendChild(text);
108 elementAcceleSpeed.appendChild(elementA6);
109
110 text = doc.createTextNode("12.25");
111 elementAA1.appendChild(text);
112 elementAASpeed.appendChild(elementAA1);
113 text = doc.createTextNode("22.25");
114 elementAA2.appendChild(text);
115 elementAASpeed.appendChild(elementAA2);
116 text = doc.createTextNode("32.25");
117 elementAA3.appendChild(text);
118 elementAASpeed.appendChild(elementAA3);
119 text = doc.createTextNode("42.25");
120 elementAA4.appendChild(text);
121 elementAASpeed.appendChild(elementAA4);
122 text = doc.createTextNode("52.25");
123 elementAA5.appendChild(text);
124 elementAASpeed.appendChild(elementAA5);
125 text = doc.createTextNode("62.25");
126 elementAA6.appendChild(text);
127 elementAASpeed.appendChild(elementAA6);
128
129 text = doc.createTextNode("12325");
130 elementP1.appendChild(text);
131 elementPosition.appendChild(elementP1);
132 text = doc.createTextNode("22325");
133 elementP2.appendChild(text);
134 elementPosition.appendChild(elementP2);
135 text = doc.createTextNode("32325");
136 elementP3.appendChild(text);
137 elementPosition.appendChild(elementP3);
138 text = doc.createTextNode("42325");
139 elementP4.appendChild(text);
140 elementPosition.appendChild(elementP4);
141 text = doc.createTextNode("52325");
142 elementP5.appendChild(text);
143 elementPosition.appendChild(elementP5);
144 text = doc.createTextNode("62325");
145 elementP6.appendChild(text);
146 elementPosition.appendChild(elementP6);
147
148 text.clear();
149
150 elementRoot.appendChild(elementSpeed);
151 elementRoot.appendChild(elementAcceleSpeed);
152 elementRoot.appendChild(elementAASpeed);
153 elementRoot.appendChild(elementPosition);
154 root.appendChild(elementRoot);
155
156
157
158 if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
159 {
160 qDebug()<<"open for add error!!";
161 }
162
163 QTextStream out(&file);
164 doc.save(out,4);
165 file.close();
166 return true;
167 }
168
169
170 bool WriteConfig::createFile(QString fileName)
171 {
172 QFile file(fileName);
173 file.open(QIODevice::ReadWrite);
174 QDomDocument doc;
175 QDomProcessingInstruction instruction;
176 instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"GB2312\"");
177 doc.appendChild(instruction);
178 QDomElement root = doc.createElement("configinfo");
179
180 doc.appendChild(root);
181 QDomText text = doc.createTextNode("");
182 root.appendChild(text);
183 QTextStream out(&file);
184 doc.save(out,4);
185 file.close();
186 }
读xml文件ReadConfig.cpp
1 bool ReadConfig::openXmlFile(QString filePath)
2 {
3 QFile file(filePath);
4 if( !file.open(QFile::ReadOnly | QFile::Text))
5 {
6 qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->file.open->%s\n") << filePath;
7
8 return false;
9 }
10
11 if( !m_doc.setContent(&file))
12 {
13 qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->doc.setContent\n") << filePath;
14
15 file.close();
16 return false;
17 }
18 file.close();
19 return true;
20 }
21
22 bool ReadConfig::changeSave()
23 {
24 QString fileName = "Motor.xml";
25 if(!openXmlFile(fileName))
26 {
27 return false;
28 }
29 //修改保存xml
30 QDomElement root = m_doc.documentElement();
31 if(root.tagName()!= "configinfo")
32 return false;
33 QDomNode n = root.firstChild();
34 while ( !n.isNull() )
35 {
36 QDomElement e = n.toElement();
37 if( !e.isNull())
38 {
39 if( e.nodeName() == "motor" )
40 {
41 if(e.hasAttributes())
42 {
43 QDomNamedNodeMap tmpMap = e.attributes();
44 for(int i = 0; i < (int)tmpMap.size();i++)
45 {
46 QString nodeName = tmpMap.item(i).nodeName();
47 QString nodeValue = tmpMap.item(i).nodeValue();
48 if("mount" == nodeName)
49 {
50 if(!nodeValue.isNull() && !nodeValue.isEmpty())
51 {
52 jointNum = nodeValue.toInt();
53 qDebug() << "************jointNum ="<<jointNum<<endl;
54 }
55 }
56 }
57 }
58 QDomNodeList list = e.childNodes(); //获得元素e的所有子节点的列表
59 for(int a=0; a<list.count(); a++) //遍历该列表
60 {
61 QDomNode node = list.at(a);
62 if(node.isElement())
63 {
64 if( node.nodeName() == "speed" )
65 {
66 if(node.hasChildNodes())
67 {
68 QDomNodeList speedList = node.childNodes();
69 for(int i = 0; i < speedList.size(); i++)
70 {
71 if("v" == speedList.at(i).toElement().tagName())
72 {
73 qDebug()<<"****speedList***"<<speedList.at(i).toElement().text().toDouble();
74 }
75 }
76 }
77 }
78 if( node.nodeName() == "accelespeed" )
79 {
80 if(node.hasChildNodes())
81 {
82 QDomNodeList speedList = node.childNodes();
83 for(int i = 0; i < speedList.size(); i++)
84 {
85 if("a" == speedList.at(i).toElement().tagName())
86 {
87 qDebug()<<"****speedList***"<<speedList.at(i).toElement().text().toDouble();
88 }
89 }
90 }
91 }
92 if(node.nodeName() == "aaspeed")
93 {
94 if(node.hasChildNodes())
95 {
96 QDomNodeList speedList = node.childNodes();
97 for(int i = 0; i < speedList.size(); i++)
98 {
99 if("aa" == speedList.at(i).toElement().tagName())
100 {
101 qDebug()<<"****speedList***"<<speedList.at(i).toElement().text().toDouble();
102 }
103 }
104 }
105 }
106 if(node.nodeName() == "position")
107 {
108 if(node.hasChildNodes())
109 {
110 QDomNodeList speedList = node.childNodes();
111 for(int i = 0; i < speedList.size(); i++)
112 {
113 if("p" == speedList.at(i).toElement().tagName())
114 {
115 qDebug()<<"****speedList***"<<speedList.at(i).toElement().text().toLongLong();
116 }
117 }
118 }
119 }
120 }
121 }
122 }
123 }
124 n = n.nextSibling();
125 }
126 QFile filexml(fileName);
127 if( !filexml.open( QFile::WriteOnly | QFile::Truncate) )
128 {
129 qWarning("error::ParserXML->writeOperateXml->file.open\n");
130 return false;
131 }
132 QTextStream ts(&filexml);
133 ts.reset();
134 ts.setCodec("utf-8");
135 m_doc.save(ts, 4, QDomNode::EncodingFromTextStream);
136 filexml.close();
137 return true;
138 }
xml文件 Motor.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <configinfo>
3 <motor mount="6">
4 <speed>
5 <v>123.25</v>
6 <v>223.25</v>
7 <v>323.25</v>
8 <v>423.25</v>
9 <v>523.25</v>
10 <v>623.25</v>
11 </speed>
12 <accelespeed>
13 <a>1023.25</a>
14 <a>2023.25</a>
15 <a>3023.25</a>
16 <a>4023.25</a>
17 <a>5023.25</a>
18 <a>6023.25</a>
19 </accelespeed>
20 <aaspeed>
21 <aa>12.25</aa>
22 <aa>22.25</aa>
23 <aa>32.25</aa>
24 <aa>42.25</aa>
25 <aa>52.25</aa>
26 <aa>62.25</aa>
27 </aaspeed>
28 <position>
29 <p>12325</p>
30 <p>22325</p>
31 <p>32325</p>
32 <p>42325</p>
33 <p>52325</p>
34 <p>62325</p>
35 </position>
36 </motor>
37 </configinfo>
|