Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29930879
  • 博文数量: 230
  • 博客积分: 2868
  • 博客等级: 少校
  • 技术积分: 2223
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-08 21:48
个人简介

Live & Learn

文章分类

全部博文(230)

文章存档

2022年(2)

2019年(5)

2018年(15)

2017年(42)

2016年(24)

2015年(13)

2014年(1)

2012年(5)

2011年(58)

2010年(56)

2009年(9)

我的朋友

分类: LINUX

2015-08-27 10:20:11

 因为需要读取配置文件,我的配置文件采用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
阅读(1539) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~