Qt 解析Xml文件

7 篇文章 0 订阅

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

设备系统软件集成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值