您的位置:首页 > 编程语言 > Qt开发

Qt读写xml文件实例

2014-08-29 10:57 555 查看
最近项目需要操作xml文件,将自己编写的读写xml文件函数附上。
//-------------------------------------------------------------------------
//写入数据到xml文件
bool CWinSystemBackup::WrInfoToXML(const QString &sFileName)
{
//设置进度
m_pProgressBar->setValue(5);
QDomDocument doc;
//增加xml的头格式
QDomProcessingInstruction instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instruction);
//增加根元素
QDomElement root = doc.createElement("Information");
doc.appendChild(root);
//--------------------IP列表----------------------
//定义IP列表子根
QDomElement IProot = doc.createElement("IPList");
root.appendChild(IProot);
//获取IP列表
QVector<QString> IPList;
g_TDeviceGroup.GetDevIpList(IPList);
for(int i = 0; i < IPList.count(); i++)
{
//增加子节点
QDomElement IPNote = doc.createElement(QString("IP%1").arg(i));
//增加子节点内容
QDomText IPNoteText = doc.createTextNode(IPList.at(i));
IPNote.appendChild(IPNoteText);
//添加子节点
IProot.appendChild(IPNote);
}
//设置进度
m_pProgressBar->setValue(10);
//---------------------------------------------------
//----------------------分辨率------------------------
//定义分辨率子根
QDomElement ResolutionRoot = doc.createElement("Resolution");
root.appendChild(ResolutionRoot);
QString text = QString("%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14").arg(
g_tDeviceInf.Nowdisplay_inf.m_nPHdsize).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPVdsize).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPFrameNum).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPHpol).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPVpol).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPHsyn).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPVsyn).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPHstart).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPVstart).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPHend).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPVend).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPHtotal).arg(
g_tDeviceInf.Nowdisplay_inf.m_nPVtotal).arg(
g_tDeviceInf.Nowdisplay_inf.m_StrnPDisName);
//增加子节点
QDomElement note = doc.createElement("resolution");
//增加子节点内容
QDomText note_text = doc.createTextNode(text);
note.appendChild(note_text);
//添加子节点
ResolutionRoot.appendChild(note);
//设置进度
m_pProgressBar->setValue(30);
//-----------------------------------------------------
//---------------------对应关系设置----------------------
//定义对应关系子根
QDomElement RelationRoot = doc.createElement("Relation");
root.appendChild(RelationRoot);
for(int i = 0; i < g_QVecUnitList.count(); i++)
{
QString text = QString("%1 %2 %3 %4 %5 %6 %7").arg(
g_QVecUnitList.at(i).m_strDevIp).arg(
g_QVecUnitList.at(i).m_StrDRouteNo).arg(
g_QVecUnitList.at(i).m_nDUnitNo).arg(
g_QVecUnitList.at(i).m_nDStartx).arg(
g_QVecUnitList.at(i).m_nDStarty).arg(
g_QVecUnitList.at(i).m_nDEndx).arg(
g_QVecUnitList.at(i).m_nDEndy);
//增加子节点
QDomElement note = doc.createElement(QString("relation%1").arg(i));
//增加子节点内容
    QDomText note_text = doc.createTextNode(text);
    note.appendChild(note_text);
//添加子节点
RelationRoot.appendChild(note);
}
//设置进度
m_pProgressBar->setValue(50);
//-----------------------------------------------------
//----------------------场景列表------------------------
//定义场景列表子根
QDomElement SceneListRoot = doc.createElement("SceneList");
root.appendChild(SceneListRoot);
for(int i = 0; i < g_tDeviceInf.SceneDeviceInf.count(); i++)
{
QString text = QString("%1 %2 %3 %4 %5").arg(
g_tDeviceInf.SceneDeviceInf.at(i).m_nSName).arg(
g_tDeviceInf.SceneDeviceInf.at(i).m_nSNameStr).arg(
g_tDeviceInf.SceneDeviceInf.at(i).m_nSFileStr).arg(
g_tDeviceInf.SceneDeviceInf.at(i).m_nSState).arg(
g_tDeviceInf.SceneDeviceInf.at(i).m_nSShortcut);
QDomElement note = doc.createElement(QString("scenelist%1").arg(i));
//增加子节点内容
    QDomText note_text = doc.createTextNode(text);
    note.appendChild(note_text);
//添加子节点
SceneListRoot.appendChild(note);
}
//设置进度
m_pProgressBar->setValue(70);
//-----------------------------------------------------
//----------------------场景信息------------------------
//定义场景信息子根
QDomElement SceneRoot = doc.createElement("SceneInformation");
root.appendChild(SceneRoot);
for(int i = 0; i < g_tDeviceInf.SceneDeviceInf.count(); i++)
{
//定义子根
QDomElement SceneChildRoot = doc.createElement(QString("SceneInfoList%1").arg(i));
SceneRoot.appendChild(SceneChildRoot);
// 加载场景信息
QString filename;
filename.sprintf("sen%05d.dat", i + 1);
QVector<TWindowParaInf> winSceneInfoList;
g_TDeviceGroup.apt_LoadSceneFromDevice(filename, winSceneInfoList, false);// false: 不替换当前场景
for(int j = 0; j < winSceneInfoList.count(); j++)
{
QString text = QString("%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11").arg(
winSceneInfoList.at(j).m_nWWinNo).arg(
winSceneInfoList.at(j).m_nWCutEnb).arg(
winSceneInfoList.at(j).m_StrWRouteNo).arg(
winSceneInfoList.at(j).m_cTWindowSiteInf.m_nSStartx).arg(
winSceneInfoList.at(j).m_cTWindowSiteInf.m_nSStarty).arg(
winSceneInfoList.at(j).m_cTWindowSiteInf.m_nSEndx).arg(
winSceneInfoList.at(j).m_cTWindowSiteInf.m_nSEndy).arg(
winSceneInfoList.at(j).m_cTCutWindowInf.m_nCStartx).arg(
winSceneInfoList.at(j).m_cTCutWindowInf.m_nCStarty).arg(
winSceneInfoList.at(j).m_cTCutWindowInf.m_nCEndx).arg(
winSceneInfoList.at(j).m_cTCutWindowInf.m_nCEndy);
QDomElement note = doc.createElement(QString("scene%1").arg(j));
//增加子节点内容
QDomText note_text = doc.createTextNode(text);
note.appendChild(note_text);
//添加子节点
SceneChildRoot.appendChild(note);
}
}
//设置进度
m_pProgressBar->setValue(90);
//-----------------------------------------------------
//删除本地文件
QFile rdfile(sFileName);
if(rdfile.open(QIODevice::ReadOnly))
{
rdfile.remove();
}
//写入数据本地新文件
QFile wrfile(sFileName);
if (!wrfile.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
{
//设置进度
m_pProgressBar->setValue(0);
QMessageBox::warning(this, tr("Warning"), tr("Save File Fail !"), QMessageBox::Yes);
return false;
}
QTextStream out(&wrfile);
out.setCodec("UTF-8");
doc.save(out, 4, QDomNode::EncodingFromTextStream);
wrfile.close();
//设置进度
m_pProgressBar->setValue(100);
return true;
}
xml文件格式如下图所示:
//-------------------------------------------------------------------------
//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("Warning"), 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, tr("Warning"), tr("Open File Fail!"), 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("Warning"), 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("Warning"), 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("Warning"), 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("Warning"), 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("Warning"), 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Qt xml 实例 c++ QDomDocument