背景:
我们每次注册后会生成对应的启动码文件,格式如下,启动码最后要在测试工具使用的进行一个验证,验证通过后模块才能使用。所以我希望每次的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 { // 没有属性的是ElementTextQString 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();