第一种读取INI配置文件
INI文件内容如下:
[conn]
ip=127.0.0.1
port=8080
databaseversion=QSQLITE
databasename=student
username=YYC
password=root
qreadini.h文件内容如下:
#ifndef QREADINI_H
#define QREADINI_H
#include<QSettings>
#define DATACONFIG QReadIni::getInstance()->getIniConfig()
typedef struct IniConfig
{
QString ip; //IP地址
QString port; //端口
QString dataBaseVersion; //数据库版本
QString dataBaseName; //数据库名称
QString userName; //用户名
QString passWord; //密码
}IniConfig;
class QReadIni
{
public:
static QReadIni*getInstance();
void readIni();
const IniConfig &getIniConfig();
private:
QReadIni();
IniConfig iniConfig;
static QReadIni*instance;
};
#endif // QREADINI_H
qreadini.cpp文件内容如下:
#include "qreadini.h"
QReadIni*QReadIni::instance = NULL;
QReadIni *QReadIni::getInstance()
{
if(instance == NULL)
{
instance = new QReadIni();
}
return instance;
}
QReadIni::QReadIni()
{
this->readIni();
}
void QReadIni::readIni()
{
QSettings * configIniRead = new QSettings("config.ini",QSettings::IniFormat);//初始化读取Ini文件对象
iniConfig.ip = configIniRead->value("conn/ip").toString(); //IP地址
iniConfig.port = configIniRead->value("conn/port").toString(); //端口
iniConfig.dataBaseVersion = configIniRead->value("conn/databaseversion").toString();//数据库版本
iniConfig.dataBaseName = configIniRead->value("conn/databasename").toString(); //数据库名称
iniConfig.userName = configIniRead->value("conn/username").toString(); //用户名
iniConfig.passWord = configIniRead->value("conn/password").toString(); //密码
delete configIniRead;
}
const IniConfig &QReadIni::getIniConfig()
{
return iniConfig;
}
第二种读取JSON文件
{
"conn": {
"ip": "127.0.0.1",
"port": "8080",
"databaseversion": "QSQLITE",
"databasename": "student",
"username": "YYC",
"password": "root"
}
}
qreadjson.h文件内容如下
#ifndef QREADJSON_H
#define QREADJSON_H
#include <QString>
#include <QFile>
#include <QIODevice>
#include <QDomNodeList>
#include <QJsonDocument>
#include <QJsonObject>
#define JSONCONFIG QReadJson::getInstance()->getJsonConfig()
typedef struct JsonConfig
{
QString ip; //IP地址
QString port; //端口
QString dataBaseVersion; //数据库版本
QString dataBaseName; //数据库名称
QString userName; //用户名
QString passWord; //密码
}JsonConfig;
class QReadJson
{
public:
static QReadJson * getInstance();
bool readJson();
const JsonConfig &getJsonConfig();
private:
QReadJson();
static QReadJson * instance;
JsonConfig jsonConfig;
};
#endif // QREADJSON_H
qreadjson.cpp内容如下:
#include"qreadjson.h"
QReadJson * QReadJson::instance = NULL;
QReadJson *QReadJson::getInstance()
{
if(NULL == instance)
{
instance = new QReadJson();
}
return instance;
}
QReadJson::QReadJson()
{
this->readJson();
}
bool QReadJson::readJson()
{
QString strJson;
QFile *file=new QFile("config.json");
if(file->open(QIODevice::ReadOnly | QIODevice::Text))
{
strJson = file->readAll();
file->close();
}
else
{
return false;
}
file->close();
QJsonDocument jsonDom = QJsonDocument::fromJson(strJson.toUtf8());
QJsonObject jsonObject = jsonDom.object();
QJsonValue jsonValue = jsonObject.value(QString("conn"));
QJsonObject objectItem = jsonValue.toObject();
jsonConfig.ip = objectItem["ip"].toString();
jsonConfig.port = objectItem["port"].toString();
jsonConfig.dataBaseVersion = objectItem["databaseversion"].toString();
jsonConfig.dataBaseName = objectItem["databasename"].toString();
jsonConfig.userName = objectItem["username"].toString();
jsonConfig.passWord = objectItem["password"].toString();
delete file;
return true;
}
const JsonConfig &QReadJson::getJsonConfig()
{
return jsonConfig;
}
第三种是读取xml文件:
<?xml version="1.0" encoding="UTF-8"?>
-<data>-
<database>
<ip>127.0.0.1</ip>
<port>8080</port>
<databaseversion>QSQLITE</databaseversion>
<databasename>student.db</databasename>
<username>root</username>
<password>123</password>
</database>
</data>
qreadxml.h文件内容如下:
#ifndef QREADXML_H
#define QREADXML_H
#include<QString>
#include<QtXml>
#include<QFile>
#include<QIODevice>
#include<QDomNodeList>
#define XMLCONFIG QReadXml::getInstance()->getXmlConfig()
typedef struct XmlConfig
{
QString ip; //IP地址
QString port; //端口
QString dataBaseVersion; //数据库版本
QString dataBaseName; //数据库名称
QString userName; //用户名
QString passWord; //密码
}XmlConfig;
class QReadXml
{
public:
static QReadXml * getInstance();
bool readXML();
const XmlConfig &getXmlConfig();
private:
QReadXml();
XmlConfig xmlConfig;
static QReadXml * instance;
};
#endif // QREADXML_H
qreadxml.cpp文件如下
#include "qreadxml.h"
QReadXml *QReadXml::instance = NULL;
QReadXml *QReadXml::getInstance()
{
if(NULL == instance)
{
instance = new QReadXml();
}
return instance;
}
QReadXml::QReadXml()
{
this->readXML();
}
bool QReadXml::readXML()
{
int countData = 0;
QDomDocument dom;
QFile *file=new QFile("config.xml");
if(file->open(QIODevice::ReadOnly))
{
if(!dom.setContent(file)) //将该xml文件导入到dom中
{
file->close();
return false;
}
}
else
{
return false;
}
file->close();
QDomElement docElem = dom.documentElement(); //返回根元素
QDomNode node = docElem.firstChild(); //返回根节点的第一个子节点
while(!node.isNull()) //如果节点不为空
{
if(node.isElement()) //如果节点是元素
{
QDomElement element=node.toElement();//将其转化为元素
QDomNodeList list=element.childNodes();//取出该元素的所有子节点放到list中
//将子节点元素全部取出来
for(int i = 0; i < list.count(); i ++)
{
QDomNode domNode = list.at(i);
if(domNode.isElement())
{
//取出我们所要的数据
switch(i)
{
case 0:xmlConfig.ip = domNode.toElement().text();break;
case 1:xmlConfig.port = domNode.toElement().text();break;
case 2:xmlConfig.dataBaseVersion = domNode.toElement().text();break;
case 3:xmlConfig.dataBaseName = domNode.toElement().text();break;
case 4:xmlConfig.userName = domNode.toElement().text();break;
case 5:xmlConfig.passWord = domNode.toElement().text();break;
}
}
}
}
countData++;
node = node.nextSibling(); //下一个兄弟节点
}
return countData > 0 ? true : false;
}
const XmlConfig &QReadXml::getXmlConfig()
{
return xmlConfig;
}
上面就是封装好的几种读取文件的几个类