读取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配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16XMLHTTPRequest项目实战入门教程
- 2024-11-14XMLHTTPRequest教程:初学者必备指南
- 2024-10-10XMLHTTPRequest项目实战:从入门到上手
- 2024-02-26lxml not found please install it
- 2022-09-16使用Druid数据源-开启监控-xml的方式-配置类的方式
- 2022-09-08IDEA中pom.xml配置文件依赖文件版本号报红的最有效解决办法
- 2022-09-05如何更换项目中web.xml的版本
- 2022-09-02使用dom4j xml解析文件数据
- 2022-08-24可扩展标记语言——XML
- 2022-08-23mybatis 配置文件mybatis.xml的加载过程