To learn how to save data into an XML file using the QDomDocument class, let's do the following:
- Add the second button to the UI, called Save XML:
- Right-click on the Save XML button and select Go to slot…. Then, pick the clicked() option and click OK. A new clicked() slot function will be added to your source files.
- Write the following code within the button's clicked() slot function:
void MainWindow::on_saveXmlButton_clicked() {
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.";
file.close();
return;
}
- The previous code continues:
QDomDocument xml("contact");
// John Doe
QDomElement root = xml.createElement("contact");
root.setAttribute("category", "Family");
xml.appendChild(root);
QDomElement tagName = xml.createElement("name");
root.appendChild(tagName);
QDomText textName = xml.createTextNode("John Doe");
tagName.appendChild(textName);
- Save the age and address elements:
QDomElement tagAge = xml.createElement("age");
root.appendChild(tagAge);
QDomText textAge = xml.createTextNode("32");
tagAge.appendChild(textAge);
QDomElement tagAddress = xml.createElement("address");
root.appendChild(tagAddress);
QDomText textAddress = xml.createTextNode("114B, 2nd Floor, Sterling Apartment, Morrisontown");
tagAddress.appendChild(textAddress);
- Let's continue with the phone element. Proceed to the next contact:
QDomElement tagPhone = xml.createElement("phone");
root.appendChild(tagPhone);
QDomText textPhone = xml.createTextNode("0221743566");
tagPhone.appendChild(textPhone);
// Jane Smith
QDomElement root2 = xml.createElement("contact");
root2.setAttribute("category", "Friend");
xml.appendChild(root2);
- Save the name and age:
QDomElement tagName2 = xml.createElement("name");
root2.appendChild(tagName2);
QDomText textName2 = xml.createTextNode("Jane Smith");
tagName2.appendChild(textName2);
QDomElement tagAge2 = xml.createElement("age");
root2.appendChild(tagAge2);
QDomText textAge2 = xml.createTextNode("24");
tagAge2.appendChild(textAge2);
- Save the address and phone:
QDomElement tagAddress2 = xml.createElement("address");
root2.appendChild(tagAddress2);
QDomText textAddress2 = xml.createTextNode("13, Ave Park, Alexandria");
tagAddress2.appendChild(textAddress2);
QDomElement tagPhone2 = xml.createElement("phone");
root2.appendChild(tagPhone2);
QDomText textPhone2 = xml.createTextNode("0025728396");
tagPhone2.appendChild(textPhone2);
- Stream all the data into the XML file:
// Save to file
QTextStream output(&file);
output << xml.toString();
file.close();
}
- Compile and run the program and click on the Save XML button. Enter your desired filename in the save file dialog and click Save.
- Open the XML file you saved in step 4 with any text editor and you should see something like this:
<!DOCTYPE contact>
<contact category="Family">
<name>John Doe</name>
<age>32</age>
<address>114B, 2nd Floor, Sterling Apartment, Morrisontown</address>
<phone>0221743566</phone>
</contact>
- We can also see the second contact under it:
<contact category="Friend">
<name>Jane Smith</name>
<age>24</age>
<address>13, Ave Park, Alexandria</address>
<phone>0025728396</phone>
</contact>