Results 1 to 3 of 3

Thread: Generate XML from a struct

  1. #1
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Generate XML from a struct

    My application maintains a struct like this (with more nesting)

    Qt Code:
    1. typedef struct
    2. {
    3. double length;
    4. double space;
    5. double tOffset;
    6. double sOffset;
    7. QString rule;
    8. }ODLD;
    9.  
    10. typedef struct
    11. {
    12. QString name;
    13. double width;
    14. ODLD odLD;
    15. }ODRMT;
    16.  
    17. typedef struct
    18. {
    19. double sOffset;
    20. QString type;
    21. QString weight;
    22. QString color;
    23. double width;
    24. QString change;
    25. QList<ODRMT*> odRMT;
    26. }ODRMR;
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Generate XML from a struct


  3. #3
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Generate XML from a struct

    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 :
    Qt Code:
    1. class OdRmtXmlNode
    2. {
    3. QDomElement _element;
    4. QDomElement _odLdElement;
    5.  
    6. public :
    7. OdRmtXmlNode(QDomElement parent,ODRMT *odrmt)
    8. {
    9. _element = parent.ownerDocument().createElement("ODRMT");
    10. parent.appendChild(_element);
    11. _element.setAttribute("name",odrmt->name);
    12. _element.setAttribute("width",odrmt->width);
    13.  
    14. _odLdElement = parent.ownerDocument().createElement("ODLD");
    15. _element.appendChild(_odLdElement);
    16. _odLdElement.setAttribute("length",odrmt->odLD.length);
    17. _odLdElement.setAttribute("space",odrmt->odLD.space);
    18. ...
    19. }
    20. };
    21.  
    22.  
    23. class OdRmrXmlDocument
    24. {
    25. QDomDocument _document;
    26. QDomElement _element;
    27. QList<OdRmtXmlNode *> _rmtNodes;
    28.  
    29. public :
    30. OdRmrXmlDocument(QDomDocument doc, const ODRMR &odrmr) :
    31. _document(doc)
    32. {
    33. _element = doc.createElement("OdRMT");
    34. doc.appendChild(_element);
    35.  
    36. _element.setAttribute("sOffset",odrmr.sOffset);
    37. ...
    38.  
    39. foreach(ODRMT *odrmt, odrmr.odRMT) _rmtNodes<<new OdRmtNode(_element, odrmt);
    40. }
    41.  
    42. ~OdRmrXmlDocument() {qDeleteAll(_rmtNodes.begin(), _rmtNodes.end());}
    43.  
    44. QString toString(int indent = -1) const{return _document.toString(indent);}
    45. };
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QList<struct>
    By Axsis in forum Newbie
    Replies: 11
    Last Post: 12th October 2015, 08:48
  2. Struct in a class
    By Atomic_Sheep in forum Newbie
    Replies: 6
    Last Post: 10th February 2012, 10:34
  3. Using global struct
    By David812 in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2011, 14:45
  4. Struct into a C++ class
    By franco.amato in forum General Programming
    Replies: 24
    Last Post: 30th September 2010, 17:13
  5. Struct in network
    By sribalaji in forum Qt Programming
    Replies: 7
    Last Post: 26th March 2008, 11:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.