Generate XML programatically in Qt

From Qt Wiki
Jump to navigation Jump to search



Overview[edit | edit source]

76ytuiytuityutyutututyutyutyu is the pendent to 76ytuiytuityutyutututyutyutyu for writing XML. Like its related class, it operates on a 76ytuiytuityutyutututyutyutyu specified with 76ytuiytuityutyutututyutyutyu. Element tags are opened with 76ytuiytuityutyutututyutyutyu followed by 76ytuiytuityutyutututyutyutyu or 76ytuiytuityutyutututyutyutyu, element content, and then 76ytuiytuityutyutututyutyutyu. A shorter form 76ytuiytuityutyutututyutyutyu can be used to write empty elements, followed by 76ytuiytuityutyutututyutyutyu.

This snippet can be self-signed.

Headers files[edit | edit source]

  1. include <QXmlStreamWriter>

class GenerateXML : public QMainWindow {


public: 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; };

Source[edit | edit source]

  1. include "GenerateXML.h"
  2. include <QMap>
  3. include <QFile>
  4. include <QMessageBox>
  5. include <QXmlStreamWriter>
  6. include <QMapIterator>

// code for constructor

GenerateXML::GenerateXML(QWidget *parent)

   : QMainWindow(parent)

{ ui.setupUi(this); 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 */ CreateXMLFile(); }

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"); } else { /*if file is successfully opened then create XML*/ QXmlStreamWriter* xmlWriter = new QXmlStreamWriter(); /* set device (here file)to streamwriter */ xmlWriter->setDevice(&file); /* Writes a document start with the XML version number version. */ xmlWriter->writeStartDocument(); /* Writes a start element with name, * Subsequent calls to writeAttribute() will add attributes to this element. here we creating a tag <persons>*/ xmlWriter->writeStartElement("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 */ xmlWriter->writeStartElement("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 */ xmlWriter->writeEndElement();

} /*end tag persons*/ xmlWriter->writeEndElement(); /*end document */ xmlWriter->writeEndDocument();

              delete xmlWriter;

} }

// code for destructor GenerateXML::~GenerateXML() {


Postconditions[edit | edit source]

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[edit | edit source]

Internal links[edit | edit source]

Code Example[edit | edit source]