分享

qt读取xml文件

 Kenshin0_0 2016-10-21

qt读取xml文件

13083阅读 1评论2012-02-23 chengxiaopeng
分类:C/C++

      因为需要读取配置文件,我的配置文件采用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; inodelist.count(); i++){
  39.     QDomNode node = nodelist.at(i);
  40.     QDomNodeList itemlist = node.childNodes(); /** 获取字节点集合 */
  41.     for(int j=0; jitemlist.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


文章评论

  • 2014-07-03tongbinwen

    你好,如果xml文档和程序接口都已经有了,怎么在qt中实现啊,

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约