PDA

View Full Version : Generate XML from a struct



pran
12th February 2013, 10:49
My application maintains a struct like this (with more nesting)


typedef struct
{
double length;
double space;
double tOffset;
double sOffset;
QString rule;
}ODLD;

typedef struct
{
QString name;
double width;
ODLD odLD;
}ODRMT;

typedef struct
{
double sOffset;
QString type;
QString weight;
QString color;
double width;
QString change;
QList<ODRMT*> odRMT;
}ODRMR;

Now I want to write this data into an XML file. Goolge-ing for this did not help me much with samples/examples. On the lookout for a sample code from where I can start of and go on to build the complete xml file.

ChrisW67
12th February 2013, 11:11
QXmlStreamWriter

thomas@itest
12th February 2013, 12:04
hi !

i'm working on quite the same things ...
i don't understand well how QXmlStreamWriter works, so, i'm using DOM elements.

what do you think about something like this :


class OdRmtXmlNode
{
QDomElement _element;
QDomElement _odLdElement;

public :
OdRmtXmlNode(QDomElement parent,ODRMT *odrmt)
{
_element = parent.ownerDocument().createElement("ODRMT");
parent.appendChild(_element);
_element.setAttribute("name",odrmt->name);
_element.setAttribute("width",odrmt->width);

_odLdElement = parent.ownerDocument().createElement("ODLD");
_element.appendChild(_odLdElement);
_odLdElement.setAttribute("length",odrmt->odLD.length);
_odLdElement.setAttribute("space",odrmt->odLD.space);
...
}
};


class OdRmrXmlDocument
{
QDomDocument _document;
QDomElement _element;
QList<OdRmtXmlNode *> _rmtNodes;

public :
OdRmrXmlDocument(QDomDocument doc, const ODRMR &odrmr) :
_document(doc)
{
_element = doc.createElement("OdRMT");
doc.appendChild(_element);

_element.setAttribute("sOffset",odrmr.sOffset);
...

foreach(ODRMT *odrmt, odrmr.odRMT) _rmtNodes<<new OdRmtNode(_element, odrmt);
}

~OdRmrXmlDocument() {qDeleteAll(_rmtNodes.begin(), _rmtNodes.end());}

QString toString(int indent = -1) const{return _document.toString(indent);}
};