qt 读取xml文件
来源:互联网 发布:cf刷烈龙软件 编辑:程序博客网 时间:2024/05/22 17:33
因为需要读取配置文件,我的配置文件采用xml;因此编写了使用qt读取xml文件内容的代码,xml文件
如下:
为了读取xml,我编写ReadConfig类代码如下:
ReadConfig.h文件内容如下
ReadConfig.cpp内容如下:
另外,因为采用Qt的xml模块,记得在你的项目pro文件中添加对xml的引用
QT += xml
如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <configuration>
- <server>
- <item key="serverip" value="222.88.1.146" />
- <item key="serverport" value="5000" />
- </server>
- </configuration>
ReadConfig.h文件内容如下
- /******************************************************************************
- *
- * 文件名: ReadConfig.h
- *
- * 文件摘要: 读取系统配置文件
- *
- * 作者:程晓鹏
- *
- * 文件创建时间: 2012/02/23 09:59:36
- *
- *******************************************************************************/
- #ifndef READCONFIG_H
- #define READCONFIG_H
- #include <QString>
- #include <QFile>
- #include <QDomDocument>
- /**
- * 读取配置文件类
- *
- */
- class ReadConfig{
- public:
-
- /**
- * 构造函数
- *
- */
- ReadConfig();
- /**
- * 析构函数
- *
- */
- ~ReadConfig();
- /**
- * 获取配置文件中的值
- *
- * @param key 配置的键
- * @param type 类型标签
- *
- * @return 配置项对应的值
- */
- QString getValue(const QString &key, const QString &type = "server");
- private:
- QFile *localfile;
- QDomDocument *dom;
- };
- #endif
- /******************************************************************************
- *
- * 文件名: ReadConfig.cpp
- *
- * 文件摘要: ReadConfig.h的实现文件
- *
- * 作者:程晓鹏
- *
- * 文件创建时间: 2012/02/23 10:07:05
- *
- *******************************************************************************/
- #include "ReadConfig.h"
- ReadConfig::ReadConfig()
- {
- QString strfilename = QString("p2p.config");
- localfile = new QFile(strfilename);
- if(!localfile->open(QFile::ReadOnly)){
- return;
- }
-
- dom = new QDomDocument();
- if(!dom->setContent(localfile)){
- localfile->close();
- return;
- }
- }
- ReadConfig::~ReadConfig()
- {
- delete localfile;
- localfile = 0;
- delete dom;
- dom = 0;
- }
- QString ReadConfig::getValue(const QString &key, const QString &type)
- {
- QString result = "";
- QDomNodeList nodelist = dom->elementsByTagName(type); /**< 读取类型节点集合 */
- for(int i=0; i<nodelist.count(); i++){
- QDomNode node = nodelist.at(i);
- QDomNodeList itemlist = node.childNodes(); /**< 获取字节点集合 */
- for(int j=0; j<itemlist.count(); j++){
- QDomNode mynode = itemlist.at(j);
- if(mynode.toElement().attribute("key") == key){ /**< 查找所需要的键值 */
- result = mynode.toElement().attribute("value");
- break;
- }
- }
- }
- return result;
- }
QT += xml
0 0
- qt 读取xml文件
- QT读取XML文件
- QT DOM方式读取xml文件(QDomElement)
- 使用Qt中的QXmlStreamReader读取XML文件
- qt中xml文件的简单读取
- QT DOM方式读取xml文件(QDomElement)
- 使用Qt中的QXmlStreamReader读取XML文件
- 利用 Qt 读取 XML 文件的方法
- QT QDomDocument读取XML文件内容
- QT读取xml例子
- QT 读取XML详解
- Qt 读取 XML 文档
- QT之XML读取
- QT SAX读取XML
- XPath在QT中的使用(QXmlQuery读取XML文件)
- qt 开发过程中使用DOM方式读取XML文件
- Qt-QML-C++交互实现文件IO系统-后继-读取XML文件和创建XML文件
- QT读取xml文件,然后写入另外一个新的xml文件
- chanel outlet store Ayten Gasson namely the altitude refinement lingerie mark which was base in 2004
- louis vuitton soldes How do you let others know what variety of folk you really are
- sac louis vuitton soldes Ralph Lauren Polo Shirts are well known bring an end to the earth The male
- replica chanel Many Plaid shirts ambition be found among a mens wardrobe They show the neutral perso
- SQL 高级(5)
- qt 读取xml文件
- 读经典之TCPL练习三
- 基于卷积完成简单的图片锐化效果
- faux louis vuitton Burberry is one of the notable refinement brands bring an end to the world Burber
- 【学习】大数据是什么
- chanel outlet Have you been out at the markets
- chanel outlet online Spring comes quietly and softly Have you made changes among your clothing apt a
- louis vuitton soldes Balenciaga has greatly benefited the modern handbags forward making Balenciaga
- kate spade sale Are you conscious of the fact that immediately you tin purchase for many Burberry ha