Results 1 to 16 of 16

Thread: Best way of creating an XML node from an object

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    99
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Best way of creating an XML node from an object

    I want my application to be able to save some of it's objects to an XML file. I have written the code below, but i'm not sure if this is the best way to do it. Surely working with XML should be simpler.
    By that i mean that the code i have is very repetitive as i have to create each node as well as the inner text node. Are there helper functions to simplify adding basic elements like this?

    Qt Code:
    1. // Creates and returns an XML node that represents SomeObject
    2. QDomElement SomeObject::asXmlNode(QDomDocument& doc) {
    3. QDomElement tempNode;
    4. QDomElement root = doc.createElement("someobject");
    5.  
    6. tempNode = doc.createElement("variable1");
    7. tempNode.appendChild(doc.createTextNode(variable1));
    8. root.appendChild(tempNode);
    9.  
    10. tempNode = doc.createElement("variable2");
    11. tempNode.appendChild(doc.createTextNode(variable2));
    12. root.appendChild(tempNode);
    13.  
    14. tempNode = doc.createElement("rectangle");
    15. tempNode .setAttribute("x", getDimensions().x());
    16. tempNode .setAttribute("y", getDimensions().y());
    17. tempNode .setAttribute("width", getDimensions().width());
    18. tempNode .setAttribute("height", getDimensions().height());
    19. root.appendChild(tempNode);
    20.  
    21. // ... etc
    22.  
    23. return root;
    24. }
    25.  
    26. // And elsewhere, write the xml node to a file
    27. QDomDocument xmlDoc;
    28. xmlDoc.appendChild(object->asXmlNode(xmlDoc));
    29.  
    30. QTextStream stream(&file);
    31. stream << xmlDoc.toString(4);
    To copy to clipboard, switch view to plain text mode 
    Last edited by xtal256; 6th April 2011 at 00:05. Reason: updated contents
    [Window Detective] - Windows UI spy utility
    [War Thunder]

Similar Threads

  1. Replies: 21
    Last Post: 28th September 2010, 10:59
  2. Replies: 10
    Last Post: 17th September 2009, 20:12
  3. how to justfy a node is leaf node or not
    By weixj2003ld in forum Qt Programming
    Replies: 4
    Last Post: 9th April 2009, 07:40
  4. Creating a QAccessibleWidget object for widget
    By Rakesh_Kumar in forum Qt Programming
    Replies: 0
    Last Post: 27th January 2009, 08:13
  5. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05

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.