Xml配置文件示例
<?xml version="1.0" encoding="utf-8"?>
<LineMap xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<inputInfo infoVec="orderNo,startTIme,endTime,unitprice"/>
<Info ID="1" inputFalg="orderNo" />
<Info ID="2" inputFalg="startTIme,endTime" />
<Info ID="3" inputFalg="unitprice" />
</LineMap>
头文件
#ifndef CDATACONFIGMANAGER_H
#define CDATACONFIGMANAGER_H
#include "dataconfigdef.h"
#include <vector>
using namespace std;
class CDataConfigManager
{
public:
static CDataConfigManager *getInstance();
bool loadVideoTutorialConfig();
private:
CDataConfigManager();
//解析XML文件
bool parse( const char *filename );
bool loadInputInfo(const char *filename);
private:
ST_INPUT_INFOS m_stInputInfos;
static CDataConfigManager *m_pInstance;
QString m_strDir;
};
#endif // CDATACONFIGMANAGER_H
源文件
#include "cdataconfigmanager.h"
#include <QFile>
#include <QTextStream>
#include <QDomDocument>
#include <QDir>
#include <QTextCodec>
#include <QDebug>
#include <QMessageBox>
#pragma execution_character_set("utf-8")
CDataConfigManager *CDataConfigManager::m_pInstance = NULL;
CDataConfigManager *CDataConfigManager::getInstance()
{
if (m_pInstance == NULL)
{
m_pInstance = new CDataConfigManager();
}
return m_pInstance;
}
CDataConfigManager::CDataConfigManager()
{
m_strDir = QDir::currentPath();
qDebug() << "strCurPath:" << m_strDir;
QString strInputInfoConfig = m_strDir + "/Config/ExampInputInfo.xml";
loadInputInfo(strInputInfoConfig.toStdString().c_str());
}
bool CDataConfigManager::loadInputInfo(const char *filename)
{
if( NULL == filename )
return false;
qDebug() << "filename:" << filename;
QFile file( filename );
if( !file.open(QFile::ReadOnly | QFile::Text) )
{
QString str= QString("loadInputInfo 打开文件:%1 失败").arg(filename);
QMessageBox msgBox;
msgBox.setText(QString::fromLocal8Bit(str.toLocal8Bit()));
msgBox.exec();
return false;
}
QTextStream stream(&file);
QTextCodec *codec=QTextCodec::codecForName("utf-8");
stream.setCodec(codec);
QString con = stream.readAll();
QDomDocument document;
document.setContent(con);
if( document.isNull() )
{
qDebug()<<"document is null";
return false;
}
QDomElement root = document.documentElement();
QDomElement domSqlConfig = root.firstChildElement();
while(!domSqlConfig.isNull())
{
if(domSqlConfig.hasAttribute("inputInfo"))
{
m_stInputInfos.strInpuInfo = domSqlConfig.attributeNode("infoVec").value();
qDebug()<<"strInpuInfo:"<<m_stInputInfos.strInpuInfo;
}
if(domSqlConfig.hasAttribute("Info"))
{
ST_INPUT_INFO stInputInfo;
stInputInfo.strID = domSqlConfig.attributeNode("ID").value();
stInputInfo.strInput = domSqlConfig.attributeNode("inputFalg").value();
qDebug()<<"strID:"<<stInputInfo.strID<<" strInput:"<<stInputInfo.strInput;
m_stInputInfos.vecInputInfo.push_back(stInputInfo);
}
domSqlConfig = domSqlConfig.nextSiblingElement();
}
return true;
}
结构体定义
typedef struct __ST_INPUT_INFOS__
{
QString strInpuInfo;
vector<ST_INPUT_INFO> vecInputInfo;
}ST_INPUT_INFOS;