其他分享
首页 > 其他分享> > Qt_——关联xml文件(获取xml文件里的节点数据)

Qt_——关联xml文件(获取xml文件里的节点数据)

作者:互联网

#include <QtXml/QDomDocument>  //xml 文件操作类

QString _getXmlSproFile_path = _getObject_path;   //文件路径
    QString _xmlfile = _getXmlSproFile_path + "/" + "ASG_gap_remesh.xml";  //要操作的文件
    QFile _openXml_file(_xmlfile);//xml 文件绝对路径
    if (!_openXml_file.open(QIODevice::ReadOnly))
    {
        qDebug() << QString::fromLocal8Bit("后处理获取密度时: xml 文件打开失败!");
        return;
    }
    //2、定义xml文档类
    QDomDocument doc;
    //3、将doc类和xml文件关联起来
        // 这个函数从IO设备dev中读取XML文档,如果内容被成功解析,则返回true;否则返回false。
    if (!doc.setContent(&_openXml_file)) {
        QMessageBox::information(NULL, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("操作的文件不是XML文件!"));
        _openXml_file.close();
        return;
    }
    // 4、关联后可以关闭文件了
    //_openXml_file.close();

    // 5、获得根节点
    QDomElement root = doc.documentElement();
    // 6、获取所有_module_List节点
    QDomNodeList _module_List = root.elementsByTagName("module");
    //qDebug() << QString::fromLocal8Bit("获取到 module 节点的个数 为:") << _module_List.size();
    for (int i = 0; i < _module_List.count(); i++)
    {
        //qDebug() << "\n" << QString::fromLocal8Bit(" 第 : ") << i + 1 << QString::fromLocal8Bit("个 build 节点 ###---------") << "\n";
        // 获取链表中的值
        QDomElement element = _module_List.at(i).toElement();
        // 找到需要读取的节点
         if (element.attribute("type") == "cavitation") // 空化模型设置
        {
            QDomNodeList sublist = element.toElement().childNodes();
            _density = sublist.at(0).toElement().attribute("liquid_density"); //介质密度(这密度 也是就胡处理 扬程工程需要的密度)
        }
    }

    _openXml_file.close();

 

标签:xml,文件,xmlfile,Qt,QString,file,path
来源: https://www.cnblogs.com/RedWetPlace/p/16521657.html