#include "WBFZAlgComponetXmlParaseOperator.h" WBFZAlgComponetXmlParamenterItem::WBFZAlgComponetXmlParamenterItem(QDomElement* itemparameter, QObject* parent) :QObject(parent) { if (nullptr == itemparameter) { qDebug() << "itemparameter is nullptr."; return; } this->ParaName = itemparameter->firstChildElement("ParaName").isNull() ? QString() : itemparameter->firstChildElement("ParaName").text(); this->ParaChsName = itemparameter->firstChildElement("ParaChsName").isNull() ? QString() : itemparameter->firstChildElement("ParaChsName").text(); this->Description = itemparameter->firstChildElement("Description").isNull() ? QString() : itemparameter->firstChildElement("Description").text(); this->Datatype = itemparameter->firstChildElement("DataType").isNull() ? QString() : itemparameter->firstChildElement("DataType").text(); this->ParaType = itemparameter->firstChildElement("ParaType").isNull() ? QString() : itemparameter->firstChildElement("ParaType").text(); this->ValueStr = itemparameter->firstChildElement("ParaValue").isNull() ? QString() : itemparameter->firstChildElement("ParaValue").text(); } WBFZAlgComponetXmlParamenterItem::~WBFZAlgComponetXmlParamenterItem() { } WBFZAlgComponetXmlParaseOperator::WBFZAlgComponetXmlParaseOperator(QObject* parent) :QObject(parent) { } WBFZAlgComponetXmlParaseOperator::~WBFZAlgComponetXmlParaseOperator() { } void WBFZAlgComponetXmlParaseOperator::loadXmlFile(const QString& fileName) { this->xmlFilePath = fileName; this->parseXmlFile(); } void WBFZAlgComponetXmlParaseOperator::writeXmlFile(const QString& formatfilepath, QString outfilepath, QString workspace, QMap inputParamslist) { // 打开输入模板xml文件 QFile inputFile(formatfilepath); if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Failed to open input XML file."; return; } // 创建QDomDocument对象 QDomDocument doc; if (!doc.setContent(&inputFile)) { qDebug() << "Failed to parse input XML file."; inputFile.close(); return; } // 关闭输入文件 inputFile.close(); // 修改工作空间路径 QDomElement root = doc.documentElement(); QDomNodeList workspacePathNodes = root.elementsByTagName("WorkSpace"); if (workspacePathNodes.count() > 0) { QDomElement workspacePathElement = workspacePathNodes.at(0).toElement(); workspacePathElement.firstChild().setNodeValue(workspace); } else { qDebug() << "WorkspacePath not found."; return; } // 修改Input标签下的Param标签下的ParaValue标签内容 QDomNodeList InputsNodes = root.elementsByTagName("Inputs"); if (InputsNodes.count() > 0) { QDomElement InputsElement = InputsNodes.at(0).toElement(); QDomNodeList parameterNodes = InputsElement.elementsByTagName("Parameter"); for (int i = 0; i < parameterNodes.count(); ++i) { QDomElement parameterElement = parameterNodes.at(i).toElement(); QString paraName = parameterElement.firstChildElement("ParaName").text(); if (inputParamslist.contains(paraName)) { QDomNodeList valueNodes = parameterElement.elementsByTagName("ParaValue"); if (valueNodes.count() > 0) { valueNodes.at(0).firstChild().setNodeValue(inputParamslist.value(paraName)); } } } } else { qDebug() << "Inputs not found."; return; } // 7. 保存修改后的XML文件 QFile outputFile(outfilepath); if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Failed to open output XML file."; return; } // 将修改后的内容写入输出文件 QTextStream out(&outputFile); out.setCodec("UTF-8"); doc.save(out, 4); // 4表示缩进级别 outputFile.close(); qDebug() << "XML file written successfully."; } void WBFZAlgComponetXmlParaseOperator::parseXmlFile() { // 1. Load the XML file QFile file(xmlFilePath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Failed to open XML file."; return; } // 2. Create a QDomDocument object QDomDocument doc; if (!doc.setContent(&file)) { qDebug() << "Failed to parse XML file."; file.close(); return; } // 3. Close the file file.close(); // 4. Get the root element QDomElement root = doc.documentElement(); // 5. Get the child elements of the root QDomNodeList childNodes = root.childNodes(); // 6. get the workspace path node QDomNodeList workspacePathNodes = root.elementsByTagName("WorkSpace"); if (workspacePathNodes.count() > 0) { QDomElement workspacePathElement = workspacePathNodes.at(0).toElement(); this->workSpacePath = workspacePathElement.text(); } else { qDebug() << "WorkspacePath not found."; return; } // 7. get Root/AlgCompt/Inputs 下的所有Parameter子节点 QDomNodeList InputsNodes = root.elementsByTagName("Inputs"); QDomNodeList parameterNodes = InputsNodes.at(0).toElement().elementsByTagName("Parameter"); if (parameterNodes.count() > 0) { for (int i = 0; i < parameterNodes.count(); ++i) { QDomElement parameterElement = parameterNodes.at(i).toElement(); WBFZAlgComponetXmlParamenterItem* item = new WBFZAlgComponetXmlParamenterItem(¶meterElement,this); this->ParameterList.append(item); qDebug() << "read Parameter Name:" << item->getParaName(); if (item->getParaName().isEmpty()) { QMessageBox::warning(nullptr, u8"警告", u8"参数名称为空,可能是xml文件格式错误"); qDebug() << "read Parameter Name Error:" << item->getParaName(); return; } } } else { qDebug() << "Parameter not found."; return; } } void WBFZAlgComponetXmlParaseOperator::displayParsedData() { } QString WBFZAlgComponetXmlParaseOperator::getWorkSpacePath() const { return this->workSpacePath; } void WBFZAlgComponetXmlParaseOperator::setWorkSpacePath(const QString& path) { this->workSpacePath = path; } QList WBFZAlgComponetXmlParaseOperator::getParameterList() const { return ParameterList; } void WBFZAlgComponetXmlParaseOperator::setParameterList(const QList& list) { this->ParameterList = list; } QString WBFZAlgComponetXmlParamenterItem::getParaName() const { return this->ParaName; } void WBFZAlgComponetXmlParamenterItem::setParaName(const QString& name) { this->ParaName = name; } QString WBFZAlgComponetXmlParamenterItem::getParaChsName() const { return this->ParaChsName; } void WBFZAlgComponetXmlParamenterItem::setParaChsName(const QString& name) { this->ParaChsName = name; } QString WBFZAlgComponetXmlParamenterItem::getDescription() const { return this->Description; } void WBFZAlgComponetXmlParamenterItem::setDescription(const QString& description) { this->Description = description; } QString WBFZAlgComponetXmlParamenterItem::getDatatype() const { return this->Datatype; } void WBFZAlgComponetXmlParamenterItem::setDatatype(const QString& datatype) { this->Datatype = datatype; } QString WBFZAlgComponetXmlParamenterItem::getParaType() const { return this->ParaType; } void WBFZAlgComponetXmlParamenterItem::setParaType(const QString& type) { this->ParaType = type; } QString WBFZAlgComponetXmlParamenterItem::getValueStr() const { return this->ValueStr; } void WBFZAlgComponetXmlParamenterItem::setValueStr(const QString& value) { this->ValueStr = value; }