How to create a new XML file when edited in the UI?
-
I have a UI which parses a value from an XML file and this value appears in the QLineEdit.When user changes the lineEdit value this should be written in the new XML file.How do I do that?I'm parsing some 20 values from XML and displaying all 20 values in the UI lineEdits.
-
Use either QXmlStreamReader + QXmlStreamWriter or QDom.
-
@sierdzio
Hi
I'm using QDom for reading.but how can i copy the complete original xml file except the edited value to the new file and the edited value should be updated in the new xml file. -
QDom holds the whole XML structure in memory. All that you need to do is to save it back to your file. Something like:
QDomDocument dom; // Your doc QFile xmlFile("path/to/your/file.xml"); if (xmlFile.open(QFile::WriteOnly | QFile::Text)) { xmlFile.write(dom.toString(4)); // Or toByteArray() }
-
@sierdzio
Hi,
I'm getting this error on using your snippet/home/kishor/mainwindow.cpp:346: error: no matching function for call to ‘QFile::write(QString)’
xmlFile.write(document.toString(4)); // Or toByteArray()What is that 4 doing there?
^ -
Read the docs I've linked. 4 creates the file with 4 space indent. https://doc.qt.io/qt-5/qdomdocument.html#toString
If that does not work, use the toByteArray(), like I've (also) said already.