Results 1 to 9 of 9

Thread: XML in Qt

  1. #1
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question XML in Qt

    Hii,

    I would like to know/have a sample code for inserting, deleting, updating nodes in an XML file.
    I tried with the QXmlStreamReader, QXmlStreamWriter but could get it done.

    Thank you,

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: XML in Qt

    For all my xml stuff I use QDomDocument,QDomNode and QDomElement. They have a pretty simple interface for doing what you require. The documentation in the links has examples for you.

  3. #3
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: XML in Qt

    i have used QDomDocument to insert node at the end but resulting output is not as desired.

    I am sending a sample code


    existing XML file ---- made use of appendChild and insertAfter

    mydocument.xml
    Qt Code:
    1. <!DOCTYPE mydocument>
    2. <root-element>
    3. <contact phone="1234567" address="ayodyanagari" name="Sita" />
    4. </root-element>
    To copy to clipboard, switch view to plain text mode 

    mydocument.xml // generated xml file
    Qt Code:
    1. <!DOCTYPE mydocument>
    2. <root-element>
    3. <contact phone="1234567" address="ayodyanagari" name="Sita" />
    4. </root-element>
    5. <root-element>
    6. <contact phone="1234567" address="ayodyanagari" name="Sita" />
    7. <contact phone="1234567" address="ayodyanagari" name="Sita" />
    8. </root-element>
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // code that generates the above xml file
    2.  
    3. QFile file("mydocument.xml");
    4. if(!file.open(QIODevice::ReadWrite))
    5. {
    6. qWarning("Open --- readwrite --err");
    7. exit (0);
    8. }
    9. //used for reading
    10. if(!doc.setContent(&file))
    11. {
    12. qWarning("setcontent --err");
    13. file.close();
    14. exit (0);
    15. }
    16.  
    17. QDomElement root = doc.documentElement();
    18.  
    19. QDomElement cn = doc.createElement("contact");
    20.  
    21. cn.setAttribute("name","Sita");
    22. cn.setAttribute("phone", "1234567");
    23. cn.setAttribute("address","ayodyanagari");
    24.  
    25. root.appendChild(cn);
    26. QTextStream ts(&file);
    27. root.save(ts,QDomNode::EncodingFromTextStream);
    To copy to clipboard, switch view to plain text mode 

    //generated file
    mydocument.xml

    Qt Code:
    1. <!DOCTYPE mydocument>
    2. <root-element>
    3. <contact phone="1234567" address="ayodyanagari" name="Sita" />
    4. </root-element>
    5. <contact phone="1234567" address="ayodyanagari" name="Sita" />
    To copy to clipboard, switch view to plain text mode 

    // code that generates the above xml file

    Qt Code:
    1. QDomNode newnode=doc.insertAfter(cn, doc.lastChild());
    2. QTextStream ts(&file);
    3. newnode.save(ts,QDomNode::EncodingFromTextStream);
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 14th July 2008 at 07:37. Reason: missing [code] tags

  4. #4
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default Re: XML in Qt

    you should write into a new file or clear old file contents then write

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML in Qt

    And save the whole document and not only the root node - otherwise you could be missing some xml declarations.

  6. #6
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: XML in Qt

    is there no other way to insert new node in an existing xml file, other than creating new xml file.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML in Qt

    The question should be: "is there no way of inserting data in the middle of an existing file" and the answer is "No, you have to recreate/rewrite the file". It's just a matter of opening the file in a different mode, you know.

  8. #8
    Join Date
    Jun 2008
    Posts
    16
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: XML in Qt

    i got a solution but it's a 3rd party support---- called libxml2 using this as well as with the GLIB support i am able to do the task... any way thanks for all the response.. Qt Centre rocks .. but guys please and if possible try to give away some code snippets.... that would be helpful for others too..

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: XML in Qt

    libxml overwrites the file as well, trust me If you want to overwrite a file, simply open the file in overwrite mode and save your contents as usual.

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.