QT xml 配置文件

之前寫過的代碼,現在又忘了,特在此記錄下QT對XML配置文件的操作,基本功能都實現了,下面的代碼僅供參考。

  • 簡介
    QT讀取、保存xml文件
  • 環境:
    QT 5.5.1、test.xml、win7 32位
  • 語言:C++

1.首先必須在xxx.pro文件中添加一行代碼:

QT  += core gui xml
  1. MyXML.h文件
#include <QObject>
#include <QFile>
#include <QMap>
#include <QDomDocument>    //涉及到的頭文件

struct StationSN
{
    QString readySN;
    QString runningSN;
    QString doneSN;
};

class MyXML
{
public:
    MyXML();

    /*************************************
     * 功能:打開xml文件
     *************************************/
    bool OpenXML();

    /*************************************
     * 功能:在元素集中查找是否有某個標籤
     * 輸入:srcElement  源元素集
     *      key         要查找的標籤
     * 輸出:element     該標籤所在的元素集合
     * 返回值:若找到則返回true
     *************************************/    
    bool FindElement(QDomElement &srcElement, QDomElement &element,QString key);

    /*************************************
     * 功能:獲取test.xml文件中的信息
     *************************************/
    void GetStationSN();

    /*************************************
     * 功能:保存到.xml文件 
     *************************************/
    void Save();

private:
    QMap<int, StationSN> stationSNMap;
    QDomDocument doc;
    QDomElement root;
};
  1. MyXML.cpp文件:
#include "myxml.h"
#include <QDebug>

MyXML::MyXML()
{
}

bool MyXML::OpenXML()
{
    QString fileName = "test.xml";
    QFile *file;
    if(!file->exists(fileName))
    {
        qDebug() << fileName << " not exist";
        return false;
    }
    else
    {
        file = new QFile(fileName);
        if(!file->open(QIODevice::ReadOnly | QFile::Text))  
        {
            qDebug() << fileName << " open filed";
            return false;
        }
        QString error;
        int row = 0, column = 0;
        if(!doc.setContent(file, false, &error, &row, &column))
        {
            qDebug() << "parse file failed at line row and column" + QString::number(row, 10) + QString(",") + QString::number(column, 10);
            file->close();
            return false;
        }
        root = doc.documentElement();
        file->close();
    }
    return true;
}

bool MyXML::FindElement(QDomElement &srcElement, QDomElement &element, QString key)
{
    QDomNode node = srcElement.firstChild();

    while(!node.isNull())
    {
        if(node.isElement())
        {
            QDomElement domElement = node.toElement();
            if( domElement.tagName()==key )
            {
                element = domElement;
                return true;
            }
        }
        node = node.nextSibling();
    }
    return false;
}

void MyXML::GetMsg()
{
    QDomElement domElement;
    if( !FindElement(root, domElement, "Station") )  //查找"Station"標籤
    {
        qDebug() << "not found Station in test.xml";
        return;
    }
    QDomNodeList list = root.childNodes();
    for(int i=0; i<list.count(); i++)
    {
        QDomElement dom;
        dom = list.at(i).toElement();
        QDomNodeList nodeList = dom.childNodes();
        stationSNMap[i].readySN = nodeList.at(1).toElement().text();
        stationSNMap[i].runningSN = nodeList.at(2).toElement().text();
        stationSNMap[i].doneSN = nodeList.at(3).toElement().text();
    }
}

void SaveData::Save()
{
    if(!OpenXML())
    {
        return;
    }
    //root是xml中的根,即<SN>標籤
    QDomNodeList list = root.elementsByTagName("Station");

    //此處修改的是<SN>標籤下第2個(索引從0開始)<Station>標籤下第3個元素的值,修改爲"hello"
    //修改前:<runningSN>0</runningSN>  修改後:<runningSN>hello</runningSN>
    list.at(1).toElement().childNodes().at(2).toElement().firstChild().setNodeValue("hello");

    QFile *file = new QFile("test.xml");
    if(!file->open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug() << "open for remove error!";
    }
    QTextStream out(file);
    doc.save(out,4);
    file->close();
}

4.test.xml文件

<?xml version='1.0' encoding='UTF-8'?>
<SN>
    <Station>
        <index>0</index>
        <readySN>0</readySN>
        <runningSN>0</runningSN>
        <doneSN>0</doneSN>
    </Station>
    <Station>
        <index>1</index>
        <readySN>0</readySN>
        <runningSN>hello</runningSN>
        <doneSN>0</doneSN>
    </Station>
    <Station>
        <index>2</index>
        <readySN>0</readySN>
        <runningSN>0</runningSN>
        <doneSN>0</doneSN>
    </Station>
 </SN>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章