最近研究了一下qt下对xml文件的读写,小计一下,成为自己的知识。
main函数调用:
#include <QApplication>
#include "readconfig.h"
#include "writeconfig.h" int main(int argc,char **argv)
{
QApplication a(argc,argv); //ReadConfig readConfig; //readConfig.changeSave(); WriteConfig writeConfig; QFile file;
QString fileName = "Motor.xml";
if(!file.exists(fileName))
{
writeConfig.createFile(fileName);
} writeConfig.writeFile(fileName); a.exec(); return ;
}
写入xml文件WriteConfig.cpp
#include "writeconfig.h" WriteConfig::WriteConfig()
{
} bool WriteConfig::writeFile(QString fileName)
{
QFile file(fileName); if(!file.open(QFile::ReadOnly | QFile::Text))
{
qDebug()<<"open for add error...";
return false;
} QDomDocument doc; QString errorStr;
int errorLine;
int errorColumn; if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
{
qDebug()<<"add setcontent error..." <<"errorStr&&&&"<<errorStr<<"errorLine"<<errorLine<<"errorColume"<<errorColumn;
//file.close();
} file.close(); QDomElement root = doc.documentElement();
if(root.isNull())
{
root = doc.createElement("configinfo");
}
QDomElement elementRoot = doc.createElement("motor");
elementRoot.setAttribute("mount","");
QDomElement elementSpeed = doc.createElement("speed");
QDomElement elementAcceleSpeed= doc.createElement("accelespeed");
QDomElement elementAASpeed = doc.createElement("aaspeed");
QDomElement elementPosition = doc.createElement("position"); QDomElement elementV1 = doc.createElement("v");
QDomElement elementV2 = doc.createElement("v");
QDomElement elementV3 = doc.createElement("v");
QDomElement elementV4 = doc.createElement("v");
QDomElement elementV5 = doc.createElement("v");
QDomElement elementV6 = doc.createElement("v");
QDomElement elementA1 = doc.createElement("a");
QDomElement elementA2 = doc.createElement("a");
QDomElement elementA3 = doc.createElement("a");
QDomElement elementA4 = doc.createElement("a");
QDomElement elementA5 = doc.createElement("a");
QDomElement elementA6 = doc.createElement("a");
QDomElement elementAA1 = doc.createElement("aa");
QDomElement elementAA2 = doc.createElement("aa");
QDomElement elementAA3 = doc.createElement("aa");
QDomElement elementAA4 = doc.createElement("aa");
QDomElement elementAA5 = doc.createElement("aa");
QDomElement elementAA6 = doc.createElement("aa");
QDomElement elementP1 = doc.createElement("p");
QDomElement elementP2 = doc.createElement("p");
QDomElement elementP3 = doc.createElement("p");
QDomElement elementP4 = doc.createElement("p");
QDomElement elementP5 = doc.createElement("p");
QDomElement elementP6 = doc.createElement("p"); QDomText text;
text = doc.createTextNode("123.25");
elementV1.appendChild(text);
elementSpeed.appendChild(elementV1);
text = doc.createTextNode("223.25");
elementV2.appendChild(text);
elementSpeed.appendChild(elementV2);
text = doc.createTextNode("323.25");
elementV3.appendChild(text);
elementSpeed.appendChild(elementV3);
text = doc.createTextNode("423.25");
elementV4.appendChild(text);
elementSpeed.appendChild(elementV4);
text = doc.createTextNode("523.25");
elementV5.appendChild(text);
elementSpeed.appendChild(elementV5);
text = doc.createTextNode("623.25");
elementV6.appendChild(text);
elementSpeed.appendChild(elementV6); text = doc.createTextNode("1023.25");
elementA1.appendChild(text);
elementAcceleSpeed.appendChild(elementA1);
text = doc.createTextNode("2023.25");
elementA2.appendChild(text);
elementAcceleSpeed.appendChild(elementA2);
text = doc.createTextNode("3023.25");
elementA3.appendChild(text);
elementAcceleSpeed.appendChild(elementA3);
text = doc.createTextNode("4023.25");
elementA4.appendChild(text);
elementAcceleSpeed.appendChild(elementA4);
text = doc.createTextNode("5023.25");
elementA5.appendChild(text);
elementAcceleSpeed.appendChild(elementA5);
text = doc.createTextNode("6023.25");
elementA6.appendChild(text);
elementAcceleSpeed.appendChild(elementA6); text = doc.createTextNode("12.25");
elementAA1.appendChild(text);
elementAASpeed.appendChild(elementAA1);
text = doc.createTextNode("22.25");
elementAA2.appendChild(text);
elementAASpeed.appendChild(elementAA2);
text = doc.createTextNode("32.25");
elementAA3.appendChild(text);
elementAASpeed.appendChild(elementAA3);
text = doc.createTextNode("42.25");
elementAA4.appendChild(text);
elementAASpeed.appendChild(elementAA4);
text = doc.createTextNode("52.25");
elementAA5.appendChild(text);
elementAASpeed.appendChild(elementAA5);
text = doc.createTextNode("62.25");
elementAA6.appendChild(text);
elementAASpeed.appendChild(elementAA6); text = doc.createTextNode("");
elementP1.appendChild(text);
elementPosition.appendChild(elementP1);
text = doc.createTextNode("");
elementP2.appendChild(text);
elementPosition.appendChild(elementP2);
text = doc.createTextNode("");
elementP3.appendChild(text);
elementPosition.appendChild(elementP3);
text = doc.createTextNode("");
elementP4.appendChild(text);
elementPosition.appendChild(elementP4);
text = doc.createTextNode("");
elementP5.appendChild(text);
elementPosition.appendChild(elementP5);
text = doc.createTextNode("");
elementP6.appendChild(text);
elementPosition.appendChild(elementP6); text.clear(); elementRoot.appendChild(elementSpeed);
elementRoot.appendChild(elementAcceleSpeed);
elementRoot.appendChild(elementAASpeed);
elementRoot.appendChild(elementPosition);
root.appendChild(elementRoot); if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open for add error!!";
} QTextStream out(&file);
doc.save(out,);
file.close();
return true;
} bool WriteConfig::createFile(QString fileName)
{
QFile file(fileName);
file.open(QIODevice::ReadWrite);
QDomDocument doc;
QDomProcessingInstruction instruction;
instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"GB2312\"");
doc.appendChild(instruction);
QDomElement root = doc.createElement("configinfo"); doc.appendChild(root);
QDomText text = doc.createTextNode("");
root.appendChild(text);
QTextStream out(&file);
doc.save(out,);
file.close();
}
读xml文件ReadConfig.cpp
bool ReadConfig::openXmlFile(QString filePath)
{
QFile file(filePath);
if( !file.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->file.open->%s\n") << filePath; return false;
} if( !m_doc.setContent(&file))
{
qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->doc.setContent\n") << filePath; file.close();
return false;
}
file.close();
return true;
} bool ReadConfig::changeSave()
{
QString fileName = "Motor.xml";
if(!openXmlFile(fileName))
{
return false;
}
//修改保存xml
QDomElement root = m_doc.documentElement();
if(root.tagName()!= "configinfo")
return false;
QDomNode n = root.firstChild();
while ( !n.isNull() )
{
QDomElement e = n.toElement();
if( !e.isNull())
{
if( e.nodeName() == "motor" )
{
if(e.hasAttributes())
{
QDomNamedNodeMap tmpMap = e.attributes();
for(int i = ; i < (int)tmpMap.size();i++)
{
QString nodeName = tmpMap.item(i).nodeName();
QString nodeValue = tmpMap.item(i).nodeValue();
if("mount" == nodeName)
{
if(!nodeValue.isNull() && !nodeValue.isEmpty())
{
jointNum = nodeValue.toInt();
qDebug() << "************jointNum ="<<jointNum<<endl;
}
}
}
}
QDomNodeList list = e.childNodes(); //获得元素e的所有子节点的列表
for(int a=; a<list.count(); a++) //遍历该列表
{
QDomNode node = list.at(a);
if(node.isElement())
{
if( node.nodeName() == "speed" )
{
if(node.hasChildNodes())
{
QDomNodeList speedList = node.childNodes();
for(int i = ; i < speedList.size(); i++)
{
if("v" == speedList.at(i).toElement().tagName())
{
qDebug()<<"****speedList***"<<speedList.at(i).toElement().text().toDouble();
}
}
}
}
if( node.nodeName() == "accelespeed" )
{
if(node.hasChildNodes())
{
QDomNodeList speedList = node.childNodes();
for(int i = ; i < speedList.size(); i++)
{
if("a" == speedList.at(i).toElement().tagName())
{
qDebug()<<"****speedList***"<<speedList.at(i).toElement().text().toDouble();
}
}
}
}
if(node.nodeName() == "aaspeed")
{
if(node.hasChildNodes())
{
QDomNodeList speedList = node.childNodes();
for(int i = ; i < speedList.size(); i++)
{
if("aa" == speedList.at(i).toElement().tagName())
{
qDebug()<<"****speedList***"<<speedList.at(i).toElement().text().toDouble();
}
}
}
}
if(node.nodeName() == "position")
{
if(node.hasChildNodes())
{
QDomNodeList speedList = node.childNodes();
for(int i = ; i < speedList.size(); i++)
{
if("p" == speedList.at(i).toElement().tagName())
{
qDebug()<<"****speedList***"<<speedList.at(i).toElement().text().toLongLong();
}
}
}
}
}
}
}
}
n = n.nextSibling();
}
QFile filexml(fileName);
if( !filexml.open( QFile::WriteOnly | QFile::Truncate) )
{
qWarning("error::ParserXML->writeOperateXml->file.open\n");
return false;
}
QTextStream ts(&filexml);
ts.reset();
ts.setCodec("utf-8");
m_doc.save(ts, , QDomNode::EncodingFromTextStream);
filexml.close();
return true;
}
xml文件
Motor.xml
<?xml version="1.0" encoding="UTF-8"?>
<configinfo>
<motor mount="6">
<speed>
<v>123.25</v>
<v>223.25</v>
<v>323.25</v>
<v>423.25</v>
<v>523.25</v>
<v>623.25</v>
</speed>
<accelespeed>
<a>1023.25</a>
<a>2023.25</a>
<a>3023.25</a>
<a>4023.25</a>
<a>5023.25</a>
<a>6023.25</a>
</accelespeed>
<aaspeed>
<aa>12.25</aa>
<aa>22.25</aa>
<aa>32.25</aa>
<aa>42.25</aa>
<aa>52.25</aa>
<aa>62.25</aa>
</aaspeed>
<position>
<p>12325</p>
<p>22325</p>
<p>32325</p>
<p>42325</p>
<p>52325</p>
<p>62325</p>
</position>
</motor>
</configinfo>
在使用的时候一定要注意路径问题,以上代码通过编译测试。