zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    KlayGE 4.0功能规划
    Kinect for Windows SDK发布
    CENTOS下用syslogng集中管理日志并压缩保存
    Eclipse+Plugin
    struts2 获取request、session的方法
    ActiveMQ使用经验
    springmock + dbutil 用来测试数据库操作
    英语学习
    如何查看eclipse版本
    jQuery插件的开发
  • 原文地址:https://www.cnblogs.com/cy568searchx/p/3628054.html
Copyright © 2011-2022 走看看