Qt中使用DOM解析XML文件或者字符串(實例)


因為需要讀取配置文件,我的配置文件采用xml;因此編寫了使用qt讀取xml文件內容的代碼,xml文件
如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <configuration>
  3.   <server>
  4.     <item key="serverip" value="222.88.1.146" />
  5.     <item key="serverport" value="5000" />
  6.   </server>
  7. </configuration>

為了讀取xml,我編寫ReadConfig類代碼如下:
ReadConfig.h文件內容如下

  1. /******************************************************************************
  2.  * 
  3.  * 文件名: ReadConfig.h
  4.  * 
  5.  * 文件摘要: 讀取系統配置文件
  6.  * 
  7.  * 作者:程曉鵬
  8.  * 
  9.  * 文件創建時間: 2012/02/23 09:59:36
  10.  *
  11.  *******************************************************************************/
  12. #ifndef READCONFIG_H
  13. #define READCONFIG_H
  14. #include <QString>
  15. #include <QFile>
  16. #include <QDomDocument>
  17. /** 
  18.  * 讀取配置文件類
  19.  * 
  20.  */
  21. class ReadConfig{
  22.  public:
  23.   
  24.   /** 
  25.    * 構造函數
  26.    * 
  27.    */
  28.   ReadConfig();
  29.   /** 
  30.    * 析構函數
  31.    * 
  32.    */
  33.   ~ReadConfig();
  34.   /** 
  35.    * 獲取配置文件中的值
  36.    * 
  37.    * @param key 配置的鍵
  38.    * @param type 類型標簽
  39.    * 
  40.    * @return 配置項對應的值
  41.    */
  42.   QString getValue(const QString &key, const QString &type = "server");
  43.  private:
  44.   QFile *localfile;
  45.   QDomDocument *dom;
  46. };
  47. #endif

ReadConfig.cpp內容如下:

  1. /******************************************************************************
  2.  *
  3.  * 文件名: ReadConfig.cpp
  4.  * 
  5.  * 文件摘要: ReadConfig.h的實現文件
  6.  * 
  7.  * 作者:程曉鵬
  8.  * 
  9.  * 文件創建時間: 2012/02/23 10:07:05
  10.  *
  11.  *******************************************************************************/
  12. #include "ReadConfig.h"
  13. ReadConfig::ReadConfig()
  14. {
  15.   QString strfilename = QString("p2p.config");
  16.   localfile = new QFile(strfilename);
  17.   if(!localfile->open(QFile::ReadOnly)){
  18.     return;
  19.   }
  20.   
  21.   dom = new QDomDocument();
  22.   if(!dom->setContent(localfile)){
  23.     localfile->close();
  24.     return;
  25.   }
  26. }
  27. ReadConfig::~ReadConfig()
  28. {
  29.   delete localfile;
  30.   localfile = 0;
  31.   delete dom;
  32.   dom = 0;
  33. }
  34. QString ReadConfig::getValue(const QString &key, const QString &type)
  35. {
  36.   QString result = "";
  37.   QDomNodeList nodelist = dom->elementsByTagName(type);    /**< 讀取類型節點集合 */
  38.   for(int i=0; i<nodelist.count(); i++){
  39.     QDomNode node = nodelist.at(i);
  40.     QDomNodeList itemlist = node.childNodes(); /**< 獲取字節點集合 */
  41.     for(int j=0; j<itemlist.count(); j++){
  42.       QDomNode mynode = itemlist.at(j);
  43.       if(mynode.toElement().attribute("key") == key){ /**< 查找所需要的鍵值 */
  44.         result = mynode.toElement().attribute("value");
  45.         break;
  46.       }
  47.     }
  48.   }
  49.   return result;
  50. }

另外,因為采用Qt的xml模塊,記得在你的項目pro文件中添加對xml的引用

QT +=  xml


免責聲明!

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



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