QT学习之读取xml中信息

背景:

我们每次注册后会生成对应的启动码文件,格式如下,启动码最后要在测试工具使用的进行一个验证,验证通过后模块才能使用。所以我希望每次的xml都放在一个文件夹里,等我选择文件夹后,能提取所有xml中的对应信息;

我的XML格式如下,我需要将里的Pid\AId\SId\ACode等都提取出来,放在表格了;

cpp 复制代码
// 选择xml所在文件夹
void QtWidgetsApplication32::on_selecXmltDir_clicked(){
    m_dirXML = QFileDialog::getExistingDirectory(this, tr("打开XML所在文件夹"), "D:/", QFileDialog::ShowDirsOnly|QFileDialog::DontResolveSymlinks);
    ui.xmlDir->setText("xml文件夹:"+m_dirXML);
}

// 生成excel文件
void QtWidgetsApplication32::on_buildExcelFile_clicked() {
    QDir dir(m_dirXML);
    if (!dir.exists()) {
        qWarning() << "The directory does not exist!";
        return;
    }

    // 文件过滤器,这里设置为获取所有文件和文件夹
    dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);

    // 将文件和文件夹排序
    dir.setSorting(QDir::Size | QDir::Reversed);

    QString csvName = "ACCode.csv";
    QFile fileCsv(csvName); // 打开csv文件
    if (!fileCsv.open(QIODevice::WriteOnly | QFile::Text))
    {
        outPutMsg(QtDebugMsg, "QtWidgetsApplication32::buildExcelFile invoke end. 打开 1.csv 失败!");
        return;
    }

    QTextStream stream(&fileCsv);
    stream << "PR_PId,PR_AId,PR_SId,PR_ACCode,PR_QueryUrl,PR_OpTime\n"; // 写入

    foreach(QFileInfo fileInfo, dir.entryInfoList()) {
        if (fileInfo.isFile()) {
           // outPutMsg(QtDebugMsg, "file:"+ fileInfo.absoluteFilePath());
            buildExcelFile(fileInfo.absoluteFilePath(), &stream);
        }
        else if (fileInfo.isDir()) {
            outPutMsg(QtDebugMsg, "dir:" + fileInfo.absoluteFilePath());
            // 目录不处理
        }
    }  
    fileCsv.close();
    ui.csvName->setText("csv文件名:"+csvName);
    QMessageBox::information(nullptr, "提示", "生成csv文件成功!");
}

// 生成Excel文件
void QtWidgetsApplication32::buildExcelFile(QString fileNames, QTextStream * pStream) {
    int iRow = 0;
    outPutMsg(QtDebugMsg, "QtWidgetsApplication32::buildExcelFile invoke begin." + fileNames);
    QFile file(fileNames); // 打开xml文件
    if (!file.open(QIODevice::ReadOnly | QFile::Text))
    {
        outPutMsg(QtDebugMsg, "QtWidgetsApplication32::buildExcelFile invoke end. 打开" + fileNames + "失败!");
        return;
    }

    QXmlStreamReader reader(&file);
    QByteArray strACCode;
    while (!reader.atEnd()) {
        QXmlStreamReader::TokenType nType = reader.readNext();
        if (nType == QXmlStreamReader::StartElement) {
            QString tagStr = reader.name().toString();
            qDebug() << reader.name().toString();
            outPutMsg(QtDebugMsg, "tagStr=" + tagStr);
            if (QString::compare(tagStr, QStringLiteral("PR_PId")) == 0){
                QXmlStreamAttributes  Attributes = reader.attributes();
                if (Attributes.hasAttribute("PId")) {
                    // 有属性的是大标签
                    outPutMsg(QtDebugMsg, "PR_PId=" + Attributes.value("PR_PId").toString());
                }
                else { 
                    // 没有属性的是ElementText
                    QString text = reader.readElementText();
                    *pStream << text;
                    *pStream << ",";
                    outPutMsg(QtDebugMsg, "text=" + text);
                }
            }
            else if (QString::compare(tagStr, QStringLiteral("ACCode")) == 0){
            }
            else if(QString::compare(tagStr, QStringLiteral("PR_OpTime")) == 0) {
                QString text = reader.readElementText();
                *pStream << text;
                *pStream << "\n";
                // 最后一个数据 做一个写cvs文件处理
                outPutMsg(QtDebugMsg, "PR_OpTime=" + text);
            }
            else{
                QString text = reader.readElementText();
                *pStream << text;
                *pStream << ",";
                outPutMsg(QtDebugMsg, tagStr+"=" + text);
            }

        }
        else{
            if (reader.isStartDocument()){
                qDebug() << reader.documentVersion().toString();
                qDebug() << reader.documentEncoding().toString();
            }
            if (reader.isComment()) {
            }
        }
    }
    QString filename = QString::fromLatin1(strACCode);
    outPutMsg(QtDebugMsg, filename);
    file.close();
    outPutMsg(QtDebugMsg, "QtWidgetsApplication32::buildExcelFile invoke end.");
}

如上所示,取属性使用QXmlStreamAttributes Attributes = reader.attributes();

取内容使用QString text = reader.readElementText();每次取完就没有了;

我提取的信息是存放在csv文件中的,如下:

QFile fileCsv(csvName); // 打开csv文件

QTextStream stream(&fileCsv);

stream << "PR_PId,PR_AId,PR_SId,PR_ACCode,PR_QueryUrl,PR_OpTime\n"; // 写入

fileCsv.close();

相关推荐
('-')1 天前
《从根上理解MySQL是怎样运行的》第十章学习笔记
笔记·学习·mysql
hd51cc1 天前
MFC学习笔记 对话框
笔记·学习·mfc
Radan小哥1 天前
Docker学习笔记—day0010
笔记·学习·docker
im_AMBER1 天前
Canvas架构手记 05 鼠标事件监听 | 原生事件封装 | ctx 结构化对象
前端·笔记·学习·架构
老神在在0011 天前
Mybatis01
后端·学习·spring·java-ee·mybatis
Y***89081 天前
Neo4j图数据库学习(二)——SpringBoot整合Neo4j
数据库·学习·neo4j
理人综艺好会1 天前
MySQL学习之go-mysql
学习·mysql·golang
寻找华年的锦瑟1 天前
Qt-视频九宫格布局
开发语言·qt
想要成为计算机高手1 天前
π*0.6: 从实践中学习 -- 2025.11.17 -- Physical Intelligence (π) -- 未开源
人工智能·学习·机器人·多模态·具身智能·vla
黑客思维者1 天前
LLM底层原理学习笔记:模型评估的基准测试体系与方法论
人工智能·笔记·神经网络·学习·模型评估·基准测试