qt 读取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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值