Results 1 to 7 of 7

Thread: XML writing problem

  1. #1
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default XML writing problem

    I used DOM to parse a XML file. I find a specified tag and add some childnodes under it. Then I called save() to convert the tree back to XML.
    Pronlem is like this:
    Original XML file:
    [HTML]<doc>
    <quote>Ars longa vita brevis</quote>
    <translation>Art is long, life is short</translation>
    </doc>[/HTML]
    After modified the tree in memory and called save():
    [HTML]<doc>
    <quote>Ars longa vita brevis</quote>
    <translation>Art is long, life is short</translation>
    </doc>
    <doc>
    <quote>Ars longa vita brevis</quote>
    <translation>Art is long, life is short</translation>
    <translation2>Art is long, life is short</translation2>
    </doc>[/HTML]

    The original file is duplicated!
    Any hint is appreciated!

  2. #2
    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 writing problem

    Could we see the code?

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: XML writing problem

    Well, QDomNode::save() operates on a stream. If you first read from a stream and then write to it, writing continues where reading was left. You might want to consider re-opening the file as truncated (seeking to position 0 is not enough since new content could be shorter which would leave garbage at the end of the file).
    J-P Nurmi

  4. #4
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML writing problem

    the code is liek this:
    Qt Code:
    1. QFile fout("Resources/file_out.xml");
    2. QDomDocument doc_out;
    3. QMap<QString, QString> IED_IPAdrr_Pair;
    4.  
    5. if (!doc_out.setContent(fout, true, &errorStr, &errorLine, &errorColumn)) {
    6. QMessageBox::warning(0, QObject::tr("DOM Parser"),
    7. QObject::tr("Parse error at line %1, "
    8. "column %2:\n%3")
    9. .arg(errorLine)
    10. .arg(errorColumn)
    11. .arg(errorStr));
    12. return;
    13. }
    14.  
    15. const int Indent = 4;
    16. QDomNode root = doc_out.firstChild();
    17. QDomNode node = findTag(root , "RemoteAddressList");
    18. QMap<QString, QString>::const_iterator i = IED_IPAdrr_Pair.constBegin();
    19. while (i != IED_IPAdrr_Pair.constEnd()) {
    20. QDomText t2 = doc_out.createTextNode(i.key());
    21. QDomText t3 = doc_out.createTextNode(i.value());
    22.  
    23. QDomElement e1 = doc_out.createElement("RemoteAddress");
    24.  
    25. QDomElement e2 = doc_out.createElement("AR_Name");
    26. e2.appendChild(t2);
    27.  
    28. QDomElement e3 = doc_out.createElement("NetAddr");
    29. e3.setAttribute("Type", "IPADRR");
    30. e3.appendChild(t3);
    31.  
    32. e1.appendChild(e2);
    33. e1.appendChild(e3);
    34.  
    35. node.appendChild(e1);
    36.  
    37. ++i;
    38. }
    39.  
    40. QTextStream out(fout);
    41. doc_out.save(out, Indent);
    To copy to clipboard, switch view to plain text mode 

    here is the declaration of findTag():
    Qt Code:
    1. QDomNode findTag(QDomNode &node, const QString &tag);
    To copy to clipboard, switch view to plain text mode 
    it find the specified tag name and return with the a QDomNode

  5. #5
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML writing problem

    Basically, I used QDomDocument::setContent(QIODevice * dev,...) to get a QDomDocument object tree in memory, then I modified this tree, then called void QDomNode::save ( QTextStream & str, int indent ) const to write this tree back to the QFile object.

    The problem may exists that I need to "clear" the original file first.
    Last edited by Shawn; 22nd October 2007 at 09:42.

  6. #6
    Join Date
    Jul 2007
    Posts
    104
    Thanks
    22
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: XML writing problem

    Actually I asked this question ago.I think there is no way for updating info in xml for now.you can only write all doc again.first delete old file(open as W mode) then write.

  7. The following user says thank you to hgedek for this useful post:

    Shawn (24th October 2007)

  8. #7
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: XML writing problem

    Yes, that's also my thinking.

    Actually this is exactly what I've done after posting my last thread. :-)

Similar Threads

  1. problem in writing text with QFile
    By wei243 in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2007, 14:26
  2. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  3. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. slow writing on flash disk
    By ir210 in forum KDE Forum
    Replies: 15
    Last Post: 13th February 2006, 15:40

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.