QT学习之读取xml中信息

该文章描述了一个程序,通过读取指定文件夹内的XML文件,使用QXmlStreamReader提取PID、AID、SID、ACode等信息,并将结果写入CSV文件。作者使用QFile、QTextStream等工具进行文件操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

在这里插入图片描述

// 选择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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值