Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 01:13, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Generate XML programatically in Qt

From Wiki
Jump to: navigation, search

This code snippet shows how to generate XML using QXmlStreamWriter. The QXmlStreamWriter class provides an XML writer with a simple streaming API.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 XpressMusic
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Platform Security
Signing Required: Self-Signed
Capabilities: None
Keywords: QXmlStreamWriter
Created: savaj (13 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)



QXmlStreamWriter is the pendent to QXmlStreamReader for writing XML. Like its related class, it operates on a QIODevice specified with setDevice(). Element tags are opened with writeStartElement() followed by writeAttribute() or writeAttributes(), element content, and then writeEndElement(). A shorter form writeEmptyElement() can be used to write empty elements, followed by writeAttributes().

This snippet can be self-signed.

Headers files

#include <QXmlStreamWriter>
class GenerateXML : public QMainWindow
GenerateXML(QWidget *parent = 0);
//call this function to create xml
void CreateXMLFile();
Ui::GenerateXMLClass ui;
//variable having data from which we have to generate xml.
QMap<QString,QString> firstname;
QMap<QString,QString> surname;
QMap<QString,QString> phoneNumber;


#include "GenerateXML.h"
#include <QMap>
#include <QFile>
#include <QMessageBox>
#include <QXmlStreamWriter>
#include <QMapIterator>
// code for constructor
GenerateXML::GenerateXML(QWidget *parent)
: QMainWindow(parent)
setWindowTitle("Generate XML");
/* Assuming user rerived data from which he wants to creare XML.
* lets we have data of 5 user to create XML.

firstname.insertMulti("firstname", "abc");
firstname.insertMulti("firstname", "ghi");
firstname.insertMulti("firstname", "mno");
firstname.insertMulti("firstname", "stu");
firstname.insertMulti("firstname", "yza");
surname.insertMulti("surname", "def");
surname.insertMulti("surname", "jkl");
surname.insertMulti("surname", "pqr");
surname.insertMulti("surname", "vwx");
surname.insertMulti("surname", "bcd");
phoneNumber.insertMulti("number", "12345");
phoneNumber.insertMulti("number", "67890");
phoneNumber.insertMulti("number", "12356");
phoneNumber.insertMulti("number", "87654");
phoneNumber.insertMulti("number", "756776");
/* call method to generate XML */
void GenerateXML::CreateXMLFile()
/* For testing purpose, we generate in C drives,
QFile file("c://contacts.xml");
/*open a file */

if (!
/* show wrror message if not able to open file */
QMessageBox::warning(0, "Read only", "The file is in read only mode");
/*if file is successfully opened then create XML*/
QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();
/* set device (here file)to streamwriter */
/* Writes a document start with the XML version number version. */
/* Writes a start element with name,
* Subsequent calls to writeAttribute() will add attributes to this element.
here we creating a tag <persons>*/

/* create mapiterator to iterate through maps*/
QMapIterator<QString, QString> i(firstname);
QMapIterator<QString, QString> j(surname);
QMapIterator<QString, QString> k(phoneNumber);
/*this while loop generates <person> tag with attributes and CDATA in each execution.
* currently we have 5 element in map so loop creates 5 tags.

while (i.hasNext() && j.hasNext() && k.hasNext())
/* create tag person */
/*add one attribute and its value*/
xmlWriter->writeAttribute(i.key(), i.value());
/*add one attribute and its value*/
xmlWriter->writeAttribute(j.key(), j.value());
/*add one attribute and its value*/
xmlWriter->writeAttribute(k.key(), k.value());
/*add character data to tag person */
xmlWriter->writeCharacters ("kamlesh");
/*close tag person */
/*end tag persons*/
/*end document */
delete xmlWriter;
// code for destructor


The code snippet is expected to generate XML as shown below.

  <?xml version="1.0" encoding="UTF-8" ?> 
- <persons>
<person firstname="yza" surname="bcd" number="756776">kamlesh</person>
<person firstname="stu" surname="vwx" number="87654">kamlesh</person>
<person firstname="mno" surname="pqr" number="12356">kamlesh</person>
<person firstname="ghi" surname="jkl" number="67890">kamlesh</person>
<person firstname="abc" surname="def" number="12345">kamlesh</person>

External links

Internal links

Code Example

This page was last modified on 11 October 2012, at 01:13.
587 page views in the last 30 days.