Since we have learned how to process data obtained from an XML file in the previous recipe, we will move on to learning how to save data to an XML file. We will continue with the previous example and add to it.
We will learn how to save data into an XML file through the following steps:
mainwindow.ui
and set its object name as saveXmlButton
and its label as Save XML:clicked()
option and click OK. A signal function called on_saveXmlButton_clicked()
will now be automatically added to both your mainwindow.h
and mainwindow.cpp
file by Qt:on_saveXmlButton_clicked()
function:QXmlStreamWriter xml; QString filename = QFileDialog::getSaveFileName(this, "Save Xml", ".", "Xml files (*.xml)"); QFile file(filename); if (!file.open(QFile::WriteOnly | QFile::Text)) qDebug() << "Error saving XML file."; xml.setDevice(&file); xml.setAutoFormatting(true); xml.writeStartDocument(); xml.writeStartElement("contact"); xml.writeAttribute("category", "Friend"); xml.writeTextElement("name", "John Doe"); xml.writeTextElement("age", "32"); xml.writeTextElement("address", "114B, 2nd Floor, Sterling Apartment, Morrison Town"); xml.writeTextElement("phone", "0221743566"); xml.writeEndElement(); xml.writeStartElement("contact"); xml.writeAttribute("category", "Family"); xml.writeTextElement("name", "Jane Smith"); xml.writeTextElement("age", "24"); xml.writeTextElement("address", "13, Ave Park, Alexandria"); xml.writeTextElement("phone", "0025728396"); xml.writeEndElement(); xml.writeEndDocument();
<?xml version="1.0" encoding="UTF-8"?> <contact category="Friend"> <name>John Doe</name> <age>32</age> <address>114B, 2nd Floor, Sterling Apartment, Morrison Town</address> <phone>0221743566</phone> </contact> <contact category="Family"> <name>Jane Smith</name> <age>24</age> <address>13, Ave Park, Alexandria</address> <phone>0025728396</phone> </contact>
The saving process is more or less similar to loading an XML file in the previous example. The only difference is instead of using the QXmlStreamReader
class, we switched to using the QXmlStreamWriter
class instead.
We are still using the file dialog and the QFile
class to save the XML file. This time, we have to change the open mode from QFile::ReadOnly
to QFile::WriteOnly
before passing the QFile
class to the stream writer.
Before we start writing any data to the new XML file, we must set auto formatting to true
, otherwise there will be no spacing; it also adds new lines and indentation to the XML file to make it look tidy and easier to read. However, if that is your intention (making it harder to read and edit by the user), then you can just ignore the setAutoFormatting()
function.
Next, start writing the XML file by calling writeStartDocument()
, followed by all the elements you want to save to the file, and at the end we call the writeEndDocument()
function to stop writing.
Each element must have a start and end tag in order for the reading process to work properly. The attributes of an element will be stored in the start tag, while the text data will be stored between the start and end tags.
If we're writing an element that contains a group of child elements, then we must call writeStartElement()
before writing the child elements. Then, call writeEndElement()
after saving all its child elements to close the group with an end tag. The writetextElement()
function, however, will automatically add the end tag for you so you don't have to worry about that one.
You can call the writeAttribute()
function to add an attribute to an element. There is no limit on how many attributes you can add to a particular element.
3.14.246.148