QT xml 读写
最近用QT在做一个项目,需要存储设备信息。
deviceInfo.h文件:#include<QWidget>#include<QtCore>#include<QString>#include<QFile>#include<QVector>#include<QtXml/QDomDocument>#include<QtXml/QDomNodeList>#include<QtXml/QDomElement>#include<QtXml/QDomAttr>classdevicesInfo{//Q_OBJECTpublic:devicesInfo();~devicesInfo();private:QDomDocumentdoc;QStringfileName;//内部接口private:voidreadXmlFile();//读取xml文件//外部接口public://获得ip地址的设备信息voidgetADeviceInfo(QStringip,QVector<QString>&deviceInfoVec);//刷新设备信息到xml文件中voidupdateDeviceInfoXml(QStringip,QVector<QString>&deviceInfoVec);};deviceInfo.cpp文件:devicesInfo::devicesInfo(){fileName="devices.xml";}devicesInfo::~devicesInfo(){}voiddevicesInfo::getADeviceInfo(QStringip,QVector<QString>&deviceInfoVec){//xml文件读到内存readXmlFile();//读取内存中的设备信息//获取根节点QDomElementroot=doc.documentElement();QStringtempInfo;QDomNodedeviceNode=root.firstChild();for(;!deviceNode.isNull();deviceNode=deviceNode.nextSibling()){if(deviceNode.isElement()){QDomElementelement=deviceNode.toElement();QDomNodeListdeviceInfoList=element.childNodes();QStringipAdress=deviceInfoList.at(0).toElement().text();if(ipAdress!=ip){//ip地址不匹配,则进入下次循环continue;}//ip地址匹配,则把相应的设备信息放到vector中for(inti=1;i<deviceInfoList.count();i++){QDomNodenodechild=deviceInfoList.at(i);if(nodechild.isElement()){tempInfo=nodechild.toElement().tagName()+":"+nodechild.toElement().text();deviceInfoVec.push_back(tempInfo);}}//endoffor(内循环)//获取信息后返回,没有必要继续寻找return;}}//endoffor(外循环)}
voiddevicesInfo::updateDeviceInfoXml(QStringip,QVector<QString>&deviceInfoVec){//读取xml文件到内存readXmlFile();//更新内存中的设备信息//获取根节点QDomElementroot=doc.documentElement();QStringListtempInfoList;QDomNodedeviceNode=root.firstChild();//记录每个设备中的属性信息是否成功更新,如果没有更新成功,则需要增加一个设备属性,默认是没有更新boolupdateAttrInfoBool;//xml中这次需要更新的每个设备属性对应一个bool值,这次用户更新的设备信息中仍有该属性则true,否则falseQVector<bool>xmlDeviceInfoBoolVec;qDebug()<<"inupdatefunction:"<<endl;//顺利遍历,寻找相应的ip地址,更新其中的设备信息for(;!deviceNode.isNull();deviceNode=deviceNode.nextSibling()){if(deviceNode.isElement()){QDomElementdeviceElement=deviceNode.toElement();QDomNodeListdeviceInfoList=deviceElement.childNodes();QStringipAdress=deviceInfoList.at(0).toElement().text();if(ipAdress!=ip){//ip地址不匹配,则跳出本循环,进入下次循环qDebug()<<"xml中的ip:"<<ipAdress<<"out."<<endl;continue;}//ip地址匹配//xml中的该匹配设备每个属性设置一个对应的bool值,并且默认是falsefor(inti=0;i<deviceInfoList.size();i++){booltempBool=false;xmlDeviceInfoBoolVec.push_back(tempBool);}//则把deviceInfoVec中的设备信息更新到该节点中for(inti=0;i<deviceInfoVec.size();i++){updateAttrInfoBool=false;//把vec中的设备信息以“:”分开,前面为tagname,后面为信息,并且跳过其中的空格//qDebug()<<"第"<<i<<"行设备信息:"<<deviceInfoVec[i]<<endl;tempInfoList=deviceInfoVec[i].split(":",QString::SkipEmptyParts);//qDebug()<<"ui:tagName:"<<tempInfoList[0]<<"deviceInfo:"<<tempInfoList[1]<<endl;//在内存中的ip设备信息中寻找tagName和用户输入的相匹配的,然后判断后面的信息是否一致for(intj=1;j<deviceInfoList.size();j++){//第0个是ip地址,所以从j=1开始//qDebug()<<"xml:tagName:"<<deviceInfoList.at(j).toElement().tagName()//<<"deviceInfo:"<<deviceInfoList.at(j).toElement().text()<<endl;if(tempInfoList[0]==deviceInfoList.at(j).toElement().tagName()){//主要为真,则说明,xml文件和用户输入中都还有这个属性,则这个属性应该在更新完继续存在,也肯定会更新xmlDeviceInfoBoolVec[j]=true;updateAttrInfoBool=true;if(tempInfoList[1]!=deviceInfoList.at(j).toElement().text()){//如果不一致,则更新deviceInfoList.at(j).toElement().firstChild().setNodeValue(tempInfoList[1]);//跳出本次更新设备属性信息的循环break;}}}if(updateAttrInfoBool==false){//如果没有更新成功,则增加一个设备属性QDomElementdeviceAttrInfo=doc.createElement(tempInfoList[0]);QDomTexttext=doc.createTextNode(tempInfoList[1]);deviceAttrInfo.appendChild(text);deviceElement.appendChild(deviceAttrInfo);}}//根据用户输入的设备更新信息,删除xml中有的但是更新后应该没有的for(inti=1;i<deviceInfoList.size();i++){if(xmlDeviceInfoBoolVec[i]==false){//如果为假,删除该设备属性deviceElement.removeChild(deviceInfoList.at(i));}}//内存中的设备信息写到xml文件QFilefile(fileName);if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate)){qDebug()<<"openforadderror!";}QTextStreamout(&file);doc.save(out,4);file.close();//返回return;}}//endoffor(外循环)}参考资料:http://www.cnblogs.com/cy568searchx/p/3628601.html
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。