QT讀取xml配置文件


//獲取字符串字段
QString ConfigHelper::GetStringConfigValue(QString str) {

    if(str == "InitDeviceNo")
    {
        return getMacPath();
    }else
    {
        QString filePath=QString("%1/%2").arg( qApp->applicationDirPath()).arg("config.xml");
        QString names=QString::null;
        ////打開或創建文件
        QFile file(filePath); //相對路徑、絕對路徑、資源路徑都行
        if(!file.open(QFile::ReadOnly))
        {

        }
        QDomDocument doc;
        if(!doc.setContent(&file))
        {
            file.close();
        }
        file.close();
        QDomElement root=doc.documentElement(); //返回根節點
        QDomNode node=root.firstChild(); //獲得第一個子節點
        while(!node.isNull())  //如果節點不空
        {
            if(node.isElement()) //如果節點是元素
            {

                QDomElement e=node.toElement(); //轉換為元素,注意元素和節點是兩個數據結構,其實差不多
                if(e.attribute("key")==str)
                {

                    names=e.attribute("value");
                   break;
                }

            }
            node=node.nextSibling(); //下一個兄弟節點,nextSiblingElement()是下一個兄弟元素,都差不多
        }
        return names;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM