read a write a XML file withe QXmlStreamWriter/QXmlStreamReader
-
Hello Every One,
1 - I created a XML file using QXmlStreamWriter class , the file containe the data of my user interface ( lineEdit) ( when we click on the Save button)
2- and now i wante to load the data (when we click on the load button),
i'm trying to load the data using a QXmlStreamReader, but without success ,
may be some one can helpe me
thanks in advanceHere is the XML file writing code.
<<QString sFileXMLName = QFileDialog::getSaveFileName(this, tr(" Xml File"), "C:/ ...", tr("Fichier Xml (*.xml)")); QFile fFileXML(sFileXMLName); if(!fFileXML.open(QFile::WriteOnly | QFile::Text)) return; QXmlStreamWriter xmlWriter(&fFileXML); xmlWriter.setAutoFormatting(true); //the norme of codification XML file xmlWriter.writeStartDocument(); //Element root of xml file xmlWriter.writeStartElement("Calib"); xmlWriter.writeStartElement("CalibPt"); xmlWriter.writeTextElement("conv", ui->lineEdit->text()); xmlWriter.writeTextElement("calib",ui-> lineEdit_9 ->text()); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("CalibPt"); xmlWriter.writeTextElement("conv", ui-> lineEdit_2 ->text()); xmlWriter.writeTextElement("calib",ui-> lineEdit_10 ->text()); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("CalibPt"); xmlWriter.writeTextElement("conv", ui-> lineEdit_3 ->text()); xmlWriter.writeTextElement("calib",ui-> lineEdit_11 ->text()); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("CalibPt"); xmlWriter.writeTextElement("conv", ui-> lineEdit_4 ->text()); xmlWriter.writeTextElement("calib",ui-> lineEdit_12 ->text()); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("CalibPt"); xmlWriter.writeTextElement("conv", ui-> lineEdit_5 ->text()); xmlWriter.writeTextElement("calib",ui-> lineEdit_13 ->text()); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("CalibPt"); xmlWriter.writeTextElement("conv", ui-> lineEdit_6 ->text()); xmlWriter.writeTextElement("calib",ui-> lineEdit_14 ->text()); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("CalibPt"); xmlWriter.writeTextElement("conv", ui-> lineEdit_7 ->text()); xmlWriter.writeTextElement("calib",ui-> lineEdit_15 ->text()); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("CalibPt"); xmlWriter.writeTextElement("conv", ui-> lineEdit_8 ->text()); xmlWriter.writeTextElement("calib",ui-> lineEdit_16 ->text()); xmlWriter.writeEndElement(); //close Element xmlWriter.writeEndElement(); //close element "Root calib" xmlWriter.writeEndElement(); //Finalised the XML file xmlWriter.writeEndDocument(); //close the xml file XML to save the document fFileXML.close();
}
-
Hi
Did you see this one?
https://www.walletfox.com/course/qxmlstreamreaderexample.phpIt explains it pretty well.
-
@mrjj , thank you for your response , i found the exemple very usfule ,
@VRonin, i resolved the probleme , juste may program it's note optimized , becouse i have a lote of condition ,thanks anyways
for the curios
QString sFilePath = QFileDialog::getOpenFileName(this, tr("Open XML File ..."), "E:/", tr("XMLFile (*.xml)")); if(sFilePath.isEmpty()) { QMessageBox::critical(this, tr("Open File XML"), tr("the file is empty !")); return; } QFileInfo fileInfo(sFilePath); QString sFileXmlName = fileInfo.fileName(); // XML object QXmlStreamReader reader; //Fichier XML QFile fFileXml(sFilePath); //open XML on red only if(!fFileXml.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::critical(this, tr("Open File XML"), tr("the file is not open !"), QMessageBox::Ok); return; } if (reader.readNextStartElement()) { if (reader.name() == "Calib"){ while(reader.readNextStartElement()){ if(reader.name() == "CalibPt"){ while(reader.readNextStartElement()){ if(reader.name() == "calib"){ QString s = reader.readElementText(); ui->lineEdit_19->setText(s); } else if(reader.name() == "conv_"){ QString s = reader.readElementText(); ui->spinBox_1->setValue(s. toInt()); } else if(reader.name() == "conv_1"){ QString s = reader.readElementText(); ui->lineEdit->setText(s); } else if(reader.name() == "calib_1"){ QString s = reader.readElementText(); ui->lineEdit_9->setText(s); } else if(reader.name() == "conv_2"){ QString s = reader.readElementText(); ui->lineEdit_2->setText(s); } else if(reader.name() == "calib_2"){ QString s = reader.readElementText(); ui->lineEdit_10->setText(s); } else if(reader.name() == "conv_3"){ QString s = reader.readElementText(); ui->lineEdit_3->setText(s); } else if(reader.name() == "calib_3"){ QString s = reader.readElementText(); ui->lineEdit_11->setText(s); } else if(reader.name() == "conv_4"){ QString s = reader.readElementText(); ui->lineEdit_4->setText(s); } else if(reader.name() == "calib_4"){ QString s = reader.readElementText(); ui->lineEdit_12->setText(s); } else if(reader.name() == "conv_5"){ QString s = reader.readElementText(); ui->lineEdit_5->setText(s); } else if(reader.name() == "calib_5"){ QString s = reader.readElementText(); ui->lineEdit_13->setText(s); } else if(reader.name() == "conv_6"){ QString s = reader.readElementText(); ui->lineEdit_6->setText(s); } else if(reader.name() == "calib_6"){ QString s = reader.readElementText(); ui->lineEdit_14->setText(s); } else if(reader.name() == "conv_7"){ QString s = reader.readElementText(); ui->lineEdit_7->setText(s); } else if(reader.name() == "calib_7"){ QString s = reader.readElementText(); ui->lineEdit_15->setText(s); } else if(reader.name() == "conv_8"){ QString s = reader.readElementText(); ui->lineEdit_8->setText(s); } else if(reader.name() == "calib_8"){ QString s = reader.readElementText(); ui->lineEdit_16->setText(s); } else reader.skipCurrentElement(); } } else reader.skipCurrentElement(); } } else reader.raiseError(QObject::tr(".")); }
I have to change the names of the subchild, otherwise it will not work.