Results 1 to 13 of 13

Thread: XML saving settings

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default XML saving settings

    Hi,

    I'm trying to save an XML file that will look like this:

    Qt Code:
    1. <?xml version="1.0" encoding="ISO-8859-1"?>
    2. <!DOCTYPE MyApp Project>
    3. <Project>
    4. <Name>
    5. "Project Name"
    6. </Name>
    7. <Programs>
    8. <Program 1>
    9. <value1>
    10. "Value1"
    11. </value1>
    12. <value2>
    13. "Value2"
    14. </value2>
    15. </Program 1>
    16. </Programs>
    17. </Project>
    To copy to clipboard, switch view to plain text mode 

    Can anoyone provide me an example to write this XML? I'm reading the docs but I can't find a XML creation example.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: XML saving settings

    Hi, have a look at the description of QDomDocument. There is an example how to create a document with nodes and then simply use toString() and write the return QString to a file.

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: XML saving settings

    Hi,

    Thanks,

    What is the difference of creating an element to store a value or to set an attribute of an element? I can see that it stores the data on a different manner.

    Table that has 2 rows and 3 columns
    Qt Code:
    1. <table>
    2. <rows>
    3. 2
    4. </rows>
    5. <columns>
    6. 3
    7. </columns>
    To copy to clipboard, switch view to plain text mode 
    or:
    Qt Code:
    1. <table>
    2. <table Rows="2" Columns="3" />
    3. </table>
    To copy to clipboard, switch view to plain text mode 

    PS: I'm able to write the XML file as the second way but I'm not able to write as the first way.
    Last edited by ^NyAw^; 11th May 2009 at 18:49.
    Òscar Llarch i Galán

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: XML saving settings

    Quote Originally Posted by ^NyAw^ View Post
    What is the difference of creating an element to store a value or to set an attribute of an element?
    Well the difference between them is the way how they are stored Well, XML activist would say: attributes are worst things ever, but I don't care about it. Both ways are legal XML. But in normally one should preferre tags instead of attributes. But not for simple integer indexes. For that i use attributes. So the XML looks nicer to me. But the decision is yours...

    PS: I'm able to write the XML file as the second way but I'm not able to write as the first way.
    Qt Code:
    1. QDomDocument doc("MyML");
    2. QDomElement root = doc.createElement("table");
    3. doc.appendChild(root);
    4.  
    5. QDomElement tag = doc.createElement("rows");
    6. root.appendChild(tag);
    7. QDomText t = doc.createTextNode("2");
    8. tag.appendChild(t);
    9.  
    10. QDomElement tag2 = doc.createElement("colums");
    11. root.appendChild(tag2);
    12. QDomText t2 = doc.createTextNode("3");
    13. tag2.appendChild(t2);
    14.  
    15. QString xml = doc.toString();
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML saving settings

    there is also a QXmlStreamWriter which directly addresses issue of writing xml.

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: XML saving settings

    Quote Originally Posted by ^NyAw^ View Post
    Hi,
    Qt Code:
    1. <table>
    2. <rows>
    3. 2
    4. </rows>
    5. <columns>
    6. 3
    7. </columns>
    To copy to clipboard, switch view to plain text mode 
    In this one the <table>, <rows> and <columns> are QDomElements, while 2, 3 are QDomTexts and they are children for <rows> and <columns> respectively. So you have to make the QDomDocumentwhich represents the whole XML document, and then use QDomDocument::createXXX() where XXX can be Element, TextNode, Attr and many more (look at Lykurg's example code). Then you can build the right XML tree by appending nodes as children to another nodes.
    The QXmlStreamWriteris a different approach, where you wirte your XML from beginnig to the end. In your example it would be something like this: start writing <table>, start writing <rows>, write 3, end writing <rows>, start writing <columns>, write 3, end writing <columns>, end writing <table>.
    And I represent the Lykurg's way of using attributes :] for the <table> I would use rows and columns as attributes, and for example each item e.g. <item> would have attributes row, column but the content of item would be tags inside <item>.
    Last edited by faldzip; 11th May 2009 at 22:54. Reason: wrong tags :P
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: XML saving settings

    Hi,

    Thanks to all.
    I asked about the second way(using QDomText) because I like the way that Qt stores the translation files(*.ts) that uses this behaviour. But maybe I will mix the two versions depending on the things that I'm going to store.

    Thanks again,
    Òscar Llarch i Galán

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: XML saving settings

    Hi,

    When I write the XML file, the elements that have attributes write them into a random order?

    Qt Code:
    1. QDomElement qProject = qDoc.createElement("Project");
    2. qDoc.appendChild(qProject);
    3. qProjecte.setAttribute(QString("Version"),QString::number(m_fVersion));
    4. qProjecte.setAttribute(QString("Name"),m_qName);
    5. qProjecte.setAttribute(QString("LogEnabled"),m_bLogEnabled ? QString("true") : QString("false"));
    To copy to clipboard, switch view to plain text mode 

    The XML file stores it like:
    Qt Code:
    1. <Project LogEnabled="false" Name="Basler 1" Version="2.5" >
    To copy to clipboard, switch view to plain text mode 

    Is there anyway to force the attributes to be stored in the order that I add them?

    Thanks,
    Òscar Llarch i Galán

  9. #9
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML saving settings

    as u set attributes, they are added on a stack and then written into xml..so add the attributes in the opposite order as you write them in code.
    but QXmlStreamWriter writes in xml just like in code.so if you use that one, you can add in exactly the same order in you require

  10. #10
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: XML saving settings

    Hi,

    as u set attributes, they are added on a stack and then written into xml..so add the attributes in the opposite order as you write them in code
    The attributes are sotred randomized instead of inverted order as you said.

    Thanks,
    Òscar Llarch i Galán

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: XML saving settings

    Quote Originally Posted by ^NyAw^ View Post
    The attributes are sotred randomized
    Yepp, they are stored in a QHash (QDomNamedNodeMapPrivate::map).

  12. #12
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: XML saving settings

    Hi,

    So? I'm not getting them sorted, nor as I'm inserted them.
    Òscar Llarch i Galán

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: XML saving settings

    You can't sort them. Unfortunately.

Similar Threads

  1. Saving settings to XML
    By arturo182 in forum Qt Programming
    Replies: 2
    Last Post: 8th March 2009, 11:10
  2. Using windows proxy settings
    By schall_l in forum Qt Programming
    Replies: 0
    Last Post: 22nd October 2008, 15:05
  3. saving settings does not work
    By MarkoSan in forum Qt Programming
    Replies: 4
    Last Post: 13th June 2008, 19:29
  4. QWidget Settings advanced idea
    By MarkoSan in forum Qt Programming
    Replies: 9
    Last Post: 27th March 2008, 09:41
  5. Replies: 4
    Last Post: 1st February 2006, 17:17

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.