Results 1 to 5 of 5

Thread: xml file append updated stream instead of updating it

  1. #1
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default xml file append updated stream instead of updating it

    Hi,
    I wanna to open a xml file and update or add some elements and node into it. I've approximately done this but the problem is that it's not updating and add updated stream in end of xml file.
    I think i must clear previous stream or file and then save it but i don't know how..
    Here is my code:

    In the meanwhile how can i clear a text file??!!!
    Qt Code:
    1. QDomDocument doc("name");
    2. QFile file(filePath);
    3. if(!file.open( QFile::WriteOnly | QFile::ReadOnly | QFile::Text))
    4. return false;
    5. if(!doc.setContent(&file));
    6. {
    7. file.close();
    8. return false;
    9. }
    10.  
    11. //Parse XML Document
    12. QDomElement docElem = doc.documentElement();
    13.  
    14. //finding an element
    15. QDomElement dialsElement;
    16. dialsElement = docElem.elementsByTagName("GaugeComponents").at(0).toElement().elementsByTagName("Dials").at(0).toElement();
    17.  
    18. //creating some elements
    19. QDomElement dialElement = doc.createElement("Dial2");
    20. QDomElement x_dialElement = doc.createElement("x");
    21. x_dialElement.setNodeValue("hello");
    22.  
    23. dialElement.appendChild(x_dialElement);
    24.  
    25. dialsElement.appendChild(dialElement);
    26.  
    27. QTextStream out(&file);
    28.  
    29. doc.save(out,0);
    30.  
    31. file.close();
    To copy to clipboard, switch view to plain text mode 
    Last edited by alizadeh91; 2nd October 2012 at 19:18.

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

    Default Re: xml file append updated stream instead of updating it

    Truncate the file before writing to it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    alizadeh91 (2nd October 2012)

  4. #3
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: xml file append updated stream instead of updating it

    What the exactly Truncate does??

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,688
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: xml file append updated stream instead of updating it

    You have opened the file for read and write. You then read the file which leaves the file's current position pointing at the end. When you write that is where it writes from, i.e. it appends to the original file. Truncating the file (see QFile::resize()) resets the file size to zero and places the cursor position at zero also.

    This whole exercise would have been easier if you opened the file for reading, read it, closed it. Then, having manipulated the DOM, opened the file for writing (which implicitly truncates the file), written it, then closed it.

  6. The following user says thank you to ChrisW67 for this useful post:

    alizadeh91 (3rd October 2012)

  7. #5
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: xml file append updated stream instead of updating it

    Ok, got it

Similar Threads

  1. updated .ui file
    By sajis997 in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 22:06
  2. How to append an xml file
    By athulms in forum Newbie
    Replies: 2
    Last Post: 29th August 2011, 09:05
  3. Replies: 6
    Last Post: 6th July 2011, 20:07
  4. Append file
    By mvbhavsar in forum Newbie
    Replies: 3
    Last Post: 10th February 2011, 09:38
  5. Replies: 3
    Last Post: 25th May 2007, 08:49

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.