Qt读写xml文件实例

前端之家收集整理的这篇文章主要介绍了Qt读写xml文件实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. 最近项目需要操作xml文件,将自己编写的读写xml文件函数附上。
  1. //-------------------------------------------------------------------------
  1. //写入数据到xml文件
  1. bool CWinSystemBackup::WrInfoToXML(const QString &sFileName)
  1. {
  1. //设置进度
  1. m_pProgressBar->setValue(5);
  1.  
  1. QDomDocument doc;
  1. //增加xml的头格式
  1. QDomProcessingInstruction instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
  1. doc.appendChild(instruction);
  1.  
  1. //增加根元素
  1. QDomElement root = doc.createElement("Information");
  1. doc.appendChild(root);
  1.  
  1. //--------------------IP列表----------------------
  1. //定义IP列表子根
  1. QDomElement IProot = doc.createElement("IPList");
  1. root.appendChild(IProot);
  1.  
  1. //获取IP列表
  1. QVector<QString> IPList;
  1. g_TDeviceGroup.GetDevIpList(IPList);
  1.  
  1. for(int i = 0; i < IPList.count(); i++)
  1. {
  1. //增加子节点
  1. QDomElement IPNote = doc.createElement(QString("IP%1").arg(i));
  1. //增加子节点内容
  1. QDomText IPNoteText = doc.createTextNode(IPList.at(i));
  1. IPNote.appendChild(IPNoteText);
  1. //添加子节点
  1. IProot.appendChild(IPNote);
  1. }
  1. //设置进度
  1. m_pProgressBar->setValue(10);
  1. //---------------------------------------------------
  1.  
  1. //----------------------分辨率------------------------
  1. //定义分辨率子根
  1. QDomElement ResolutionRoot = doc.createElement("Resolution");
  1. root.appendChild(ResolutionRoot);
  1.  
  1. QString text = QString("%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14").arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPHdsize).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPVdsize).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPFrameNum).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPHPol).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPVpol).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPHsyn).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPVsyn).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPHstart).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPVstart).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPHend).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPVend).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPHtotal).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_nPVtotal).arg(
  1. g_tDeviceInf.Nowdisplay_inf.m_StrnPDisName);
  1. //增加子节点
  1. QDomElement note = doc.createElement("resolution");
  1. //增加子节点内容
  1. QDomText note_text = doc.createTextNode(text);
  1. note.appendChild(note_text);
  1. //添加子节点
  1. ResolutionRoot.appendChild(note);
  1. //设置进度
  1. m_pProgressBar->setValue(30);
  1. //-----------------------------------------------------
  1.  
  1. //---------------------对应关系设置----------------------
  1. //定义对应关系子根
  1. QDomElement RelationRoot = doc.createElement("Relation");
  1. root.appendChild(RelationRoot);
  1.  
  1. for(int i = 0; i < g_QVecUnitList.count(); i++)
  1. {
  1. QString text = QString("%1 %2 %3 %4 %5 %6 %7").arg(
  1. g_QVecUnitList.at(i).m_strDevIp).arg(
  1. g_QVecUnitList.at(i).m_StrDRouteNo).arg(
  1. g_QVecUnitList.at(i).m_nDUnitNo).arg(
  1. g_QVecUnitList.at(i).m_nDStartx).arg(
  1. g_QVecUnitList.at(i).m_nDStarty).arg(
  1. g_QVecUnitList.at(i).m_nDEndx).arg(
  1. g_QVecUnitList.at(i).m_nDEndy);
  1. //增加子节点
  1. QDomElement note = doc.createElement(QString("relation%1").arg(i));
  1. //增加子节点内容
  1. QDomText note_text = doc.createTextNode(text);
  1. note.appendChild(note_text);
  1. //添加子节点
  1. RelationRoot.appendChild(note);
  1. }
  1. //设置进度
  1. m_pProgressBar->setValue(50);
  1. //-----------------------------------------------------
  1.  
  1. //----------------------场景列表------------------------
  1. //定义场景列表子根
  1. QDomElement SceneListRoot = doc.createElement("SceneList");
  1. root.appendChild(SceneListRoot);
  1. for(int i = 0; i < g_tDeviceInf.SceneDeviceInf.count(); i++)
  1. {
  1. QString text = QString("%1 %2 %3 %4 %5").arg(
  1. g_tDeviceInf.SceneDeviceInf.at(i).m_nSName).arg(
  1. g_tDeviceInf.SceneDeviceInf.at(i).m_nSNameStr).arg(
  1. g_tDeviceInf.SceneDeviceInf.at(i).m_nSFileStr).arg(
  1. g_tDeviceInf.SceneDeviceInf.at(i).m_nSState).arg(
  1. g_tDeviceInf.SceneDeviceInf.at(i).m_nSShortcut);
  1.  
  1. QDomElement note = doc.createElement(QString("scenelist%1").arg(i));
  1. //增加子节点内容
  1. QDomText note_text = doc.createTextNode(text);
  1. note.appendChild(note_text);
  1. //添加子节点
  1. SceneListRoot.appendChild(note);
  1. }
  1. //设置进度
  1. m_pProgressBar->setValue(70);
  1. //-----------------------------------------------------
  1.  
  1. //----------------------场景信息------------------------
  1. //定义场景信息子根
  1. QDomElement SceneRoot = doc.createElement("SceneInformation");
  1. root.appendChild(SceneRoot);
  1.  
  1. for(int i = 0; i < g_tDeviceInf.SceneDeviceInf.count(); i++)
  1. {
  1. //定义子根
  1. QDomElement SceneChildRoot = doc.createElement(QString("SceneInfoList%1").arg(i));
  1. SceneRoot.appendChild(SceneChildRoot);
  1.  
  1. // 加载场景信息
  1. QString filename;
  1. filename.sprintf("sen%05d.dat", i + 1);
  1.  
  1. QVector<TWindowParaInf> winSceneInfoList;
  1. g_TDeviceGroup.apt_LoadSceneFromDevice(filename, winSceneInfoList, false);// false: 不替换当前场景
  1.  
  1. for(int j = 0; j < winSceneInfoList.count(); j++)
  1. {
  1. QString text = QString("%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11").arg(
  1. winSceneInfoList.at(j).m_nWWinNo).arg(
  1. winSceneInfoList.at(j).m_nWCutEnb).arg(
  1. winSceneInfoList.at(j).m_StrWRouteNo).arg(
  1. winSceneInfoList.at(j).m_cTWindowSiteInf.m_nSStartx).arg(
  1. winSceneInfoList.at(j).m_cTWindowSiteInf.m_nSStarty).arg(
  1. winSceneInfoList.at(j).m_cTWindowSiteInf.m_nSEndx).arg(
  1. winSceneInfoList.at(j).m_cTWindowSiteInf.m_nSEndy).arg(
  1. winSceneInfoList.at(j).m_cTCutWindowInf.m_nCStartx).arg(
  1. winSceneInfoList.at(j).m_cTCutWindowInf.m_nCStarty).arg(
  1. winSceneInfoList.at(j).m_cTCutWindowInf.m_nCEndx).arg(
  1. winSceneInfoList.at(j).m_cTCutWindowInf.m_nCEndy);
  1.  
  1. QDomElement note = doc.createElement(QString("scene%1").arg(j));
  1. //增加子节点内容
  1. QDomText note_text = doc.createTextNode(text);
  1. note.appendChild(note_text);
  1. //添加子节点
  1. SceneChildRoot.appendChild(note);
  1. }
  1. }
  1. //设置进度
  1. m_pProgressBar->setValue(90);
  1. //-----------------------------------------------------
  1.  
  1.  
  1. //删除本地文件
  1. QFile rdfile(sFileName);
  1. if(rdfile.open(QIODevice::ReadOnly))
  1. {
  1. rdfile.remove();
  1. }
  1.  
  1. //写入数据本地新文件
  1. QFile wrfile(sFileName);
  1.  
  1. if (!wrfile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
  1. {
  1. //设置进度
  1. m_pProgressBar->setValue(0);
  1. QMessageBox::warning(this, tr("Warning"), tr("Save File Fail !"), QMessageBox::Yes);
  1. return false;
  1. }
  1.  
  1. QTextStream out(&wrfile);
  1. out.setCodec("UTF-8");
  1. doc.save(out, 4, QDomNode::EncodingFromTextStream);
  1. wrfile.close();
  1.  
  1. //设置进度
  1. m_pProgressBar->setValue(100);
  1. return true;
  1. }
  1. xml文件格式如下图所示:
  1. //-------------------------------------------------------------------------
    //xml文件读取数据
    bool CWinSystemBackup::RdInfoFromXML(const QString &sFileName,
     QVector<QString> &IPList,
     TDisplayInf &tDisplayInf,
     QVector <TDisplayUnitInf> &tDisplayUnitInfList,
     QVector <TDeviceSceneInf> &tSceneList,
     QVector<QVector<TWindowParaInf> > &winSceneInfoList)
    {
     QString errorStr;
     int errorLine;
     int errorColum;
     QFile file(sFileName);
     if (!file.open(QFile::ReadOnly))
     {
     QMessageBox::warning(this, tr("Open File Fail!"), QMessageBox::Yes);
     //设置进度
     m_pProgressBar->setValue(0);
     return false;
     }
     QDomDocument doc;
     if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColum))
     {
     QMessageBox::warning(this, QMessageBox::Yes);
     //设置进度
     m_pProgressBar->setValue(0);
     return false;
     }
     //设置进度
     m_pProgressBar->setValue(5);
     QDomElement element;
     element = doc.documentElement();
     if(element.tagName() != "Information")
     {
     QMessageBox::warning(this, tr("File Format Error!"), QMessageBox::Yes);
     //设置进度
     m_pProgressBar->setValue(0);
     return false;
     }
     QDomElement elementChild = element.firstChildElement();
     while (!elementChild.isNull())
     {
     //IP列表
     if (elementChild.toElement().tagName() == "IPList")
     {
     QDomNode elementChildNode = elementChild.firstChild();
     while(!elementChildNode.isNull())
     {
     IPList.append(elementChildNode.toElement().text());
     elementChildNode = elementChildNode.nextSibling();
     }
     qDebug()<< "IPList:" << IPList << endl;
     }
     //当前分辨率
     if (elementChild.toElement().tagName() == "Resolution")
     {
     QDomNode elementChildNode = elementChild.firstChild();
     QString sResolution = elementChildNode.toElement().text();
     qDebug()<< "The resolution is :" << sResolution << endl;
     QStringList sResolutionList = sResolution.split(" ");
     if(sResolutionList.count() == 14)
     {
     tDisplayInf.m_nPHdsize = sResolutionList.at(0).toInt();
     tDisplayInf.m_nPVdsize = sResolutionList.at(1).toInt();
     tDisplayInf.m_nPFrameNum = sResolutionList.at(2).toInt();
     tDisplayInf.m_nPHPol = sResolutionList.at(3).toInt();
     tDisplayInf.m_nPVpol = sResolutionList.at(4).toInt();
     tDisplayInf.m_nPHsyn = sResolutionList.at(5).toInt();
     tDisplayInf.m_nPVsyn = sResolutionList.at(6).toInt();
     tDisplayInf.m_nPHstart = sResolutionList.at(7).toInt();
     tDisplayInf.m_nPVstart = sResolutionList.at(8).toInt();
     tDisplayInf.m_nPHend = sResolutionList.at(9).toInt();
     tDisplayInf.m_nPVend = sResolutionList.at(10).toInt();
     tDisplayInf.m_nPHtotal = sResolutionList.at(11).toInt();
     tDisplayInf.m_nPVtotal = sResolutionList.at(12).toInt();
     tDisplayInf.m_StrnPDisName = sResolutionList.at(13);
     }
     else
     {
     QMessageBox::warning(this, tr("Read Resolution Data Fail!"), QMessageBox::Yes);
     //设置进度
     m_pProgressBar->setValue(0);
     return false;
     }
     //设置进度
     m_pProgressBar->setValue(10);
     }
     //对应关系列表
     if (elementChild.toElement().tagName() == "Relation")
     {
     QVector<QString> RelationList;
     QDomNode elementChildNode = elementChild.firstChild();
     while(!elementChildNode.isNull())
     {
     RelationList.append(elementChildNode.toElement().text());
     elementChildNode = elementChildNode.nextSibling();
     }
     qDebug()<< "Relation:" << RelationList << endl;
     for(int i = 0; i < RelationList.count(); i++ )
     {
     QStringList sRelationList = RelationList.at(i).split(" ");
     if(sRelationList.count() == 7)
     {
     TDisplayUnitInf tDisPlayUnitInfo;
     tDisPlayUnitInfo.m_strDevIp = sRelationList.at(0);
     tDisPlayUnitInfo.m_StrDRouteNo = sRelationList.at(1);
     tDisPlayUnitInfo.m_nDUnitNo = sRelationList.at(2).toInt();
     tDisPlayUnitInfo.m_nDStartx = sRelationList.at(3).toInt();
     tDisPlayUnitInfo.m_nDStarty = sRelationList.at(4).toInt();
     tDisPlayUnitInfo.m_nDEndx = sRelationList.at(5).toInt();
     tDisPlayUnitInfo.m_nDEndy = sRelationList.at(6).toInt();
     tDisplayUnitInfList.append(tDisPlayUnitInfo);
     }
     else
     {
     QMessageBox::warning(this, tr("Read Relation Data Fail!"), QMessageBox::Yes);
     //设置进度
     m_pProgressBar->setValue(0);
     return false;
     }
     }
     //设置进度
     m_pProgressBar->setValue(20);
     }
     //场景列表
     if (elementChild.toElement().tagName() == "SceneList")
     {
     QVector<QString> SceneList;
     QDomNode elementChildNode = elementChild.firstChild();
     while(!elementChildNode.isNull())
     {
     SceneList.append(elementChildNode.toElement().text());
     elementChildNode = elementChildNode.nextSibling();
     }
     qDebug()<< "SceneList:" << SceneList << endl;
     for(int i = 0; i < SceneList.count(); i++ )
     {
     QStringList sSceneList = SceneList.at(i).split(" ");
     if(sSceneList.count() == 5)
     {
     TDeviceSceneInf tSceneInfo;
     tSceneInfo.m_nSName = sSceneList.at(0).toInt();
     tSceneInfo.m_nSNameStr = sSceneList.at(1);
     tSceneInfo.m_nSFileStr = sSceneList.at(2);
     tSceneInfo.m_nSState = sSceneList.at(3).toInt();
     tSceneInfo.m_nSShortcut = sSceneList.at(4).toInt();
     tSceneList.append(tSceneInfo);
     }
     else
     {
     QMessageBox::warning(this, tr("Read Scene List Data Fail!"), QMessageBox::Yes);
     //设置进度
     m_pProgressBar->setValue(0);
     return false;
     }
     }
     //设置进度
     m_pProgressBar->setValue(30);
     }
     //场景信息
     if (elementChild.toElement().tagName() == "SceneInformation")
     {
     QDomElement elementGrandson = elementChild.firstChildElement();
     if(!elementGrandson.isNull())
     {
     for(int i = 0; i < tSceneList.count(); i++)
     {
     if(elementGrandson.toElement().tagName() == QString("SceneInfoList%1").arg(i))
     {
     QVector<QString> SceneInfoList;
     QDomNode elementGrandsonNode = elementGrandson.firstChild();
     while(!elementGrandsonNode.isNull())
     {
     SceneInfoList.append(elementGrandsonNode.toElement().text());
     elementGrandsonNode = elementGrandsonNode.nextSibling();
     }
     qDebug()<< "SceneInfoList:" << SceneInfoList << endl;
     QVector<TWindowParaInf> SceneInfo;
     for(int j = 0; j < SceneInfoList.count(); j++ )
     {
     QStringList sSceneInfoList = SceneInfoList.at(j).split(" ");
     if(sSceneInfoList.count() == 11)
     {
     TWindowParaInf tWinSceneInfo;
     tWinSceneInfo.m_nWWinNo = sSceneInfoList.at(0).toInt();
     tWinSceneInfo.m_nWCutEnb = sSceneInfoList.at(1).toInt();
     tWinSceneInfo.m_StrWRouteNo = sSceneInfoList.at(2);
     tWinSceneInfo.m_cTWindowSiteInf.m_nSStartx = sSceneInfoList.at(3).toInt();
     tWinSceneInfo.m_cTWindowSiteInf.m_nSStarty = sSceneInfoList.at(4).toInt();
     tWinSceneInfo.m_cTWindowSiteInf.m_nSEndx = sSceneInfoList.at(5).toInt();
     tWinSceneInfo.m_cTWindowSiteInf.m_nSEndy = sSceneInfoList.at(6).toInt();
     tWinSceneInfo.m_cTCutWindowInf.m_nCStartx = sSceneInfoList.at(7).toInt();
     tWinSceneInfo.m_cTCutWindowInf.m_nCStarty = sSceneInfoList.at(8).toInt();
     tWinSceneInfo.m_cTCutWindowInf.m_nCEndx = sSceneInfoList.at(9).toInt();
     tWinSceneInfo.m_cTCutWindowInf.m_nCEndy = sSceneInfoList.at(10).toInt();
     SceneInfo.append(tWinSceneInfo);
     }
     else
     {
     QMessageBox::warning(this, tr("Read Scene Information Data Fail!"), QMessageBox::Yes);
     //设置进度
     m_pProgressBar->setValue(0);
     return false;
     }
     }
     winSceneInfoList.append(SceneInfo);
     }
     elementGrandson = elementGrandson.nextSiblingElement();
     }
     }
     //设置进度
     m_pProgressBar->setValue(40);
     }
     elementChild = elementChild.nextSiblingElement();
     }
     return true;
    }

猜你在找的XML相关文章