读取XML配置

2021/7/16 6:06:41

本文主要是介绍读取XML配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#include <QtXml>

private:

    const QString CONFIG_PATH = "./config.xml";
    const QString UDP_STR = "udp";
    const QString TCP_STR = "tcp";
    const QString SQL_STR = "sql";
    const QString PACK_STR = "package";
    const QString MULT_IP = "multicastIp";
    const QString PORT = "port";
    const QString LOCAL_IP = "localIp";
    const QString LOCAL_PORT = "localPort";
    const QString dbIp = "dbIp";
    const QString userName="userName";
    const QString Password="Password";
    const QString dbName="dbName";
    QByteArray _onePackage; //一个整包数据
    QUdpSocket _multicastUdp;
    QHostAddress localIp;
    uint localPort;
    uint _port;
    QHostAddress _multIp;
    QDomElement _packElement;
    QString dbip,username,password,dbname;

 void NetworkRcv::_initConfig()

{
    QDomDocument doc;
    QDomElement root; 
    QFile file(CONFIG_PATH);
    if(file.open(QFile::ReadOnly)){
        if(doc.setContent(&file)){
            root = doc.documentElement();
        }
        file.close();
    }
    for(int i=0;i<root.childNodes().size();i++){
        QDomElement secondElement = root.childNodes().at(i).toElement();
        if(secondElement.tagName() == UDP_STR)
        {
            _multIp = QHostAddress(secondElement.attribute(MULT_IP, ""));//组网地址
            _port = secondElement.attribute(PORT, "").toInt();//组网端口
            localIp = QHostAddress(secondElement.attribute(LOCAL_IP, ""));
            localPort = secondElement.attribute(LOCAL_PORT, "").toInt();
        }
        else if(secondElement.tagName()== SQL_STR)
        {
            dbip =  secondElement.attribute(dbIp, "");
            username= secondElement.attribute(userName, "");
            password= secondElement.attribute(Password, "");
            dbname =secondElement.attribute(dbName, "");
        }
    }
}


这篇关于读取XML配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程