Results 1 to 12 of 12

Thread: problem in reading xml file with SAX parser.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: problem in reading xml file with SAX parser.

    You can't append to the file like that. You need to replace the contents of the file and not append 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.


  2. #2
    Join Date
    May 2011
    Posts
    120
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    9

    Default Re: problem in reading xml file with SAX parser.

    Please be more elaborated, it is possible to give some sample code.

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

    Default Re: problem in reading xml file with SAX parser.

    If you have a document and you modify it, you need to save the whole document to the file overwriting the old version of the document. XML doesn't work in an "additive" manner. It's a matter of understanding how XML works. If you open the file in append mode, you will get two documents one after the other instead of having the updated document only.
    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.


  4. #4
    Join Date
    May 2011
    Posts
    120
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    9

    Default Re: problem in reading xml file with SAX parser.

    So what i have to do now , do i have to create temproryfile.

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

    Default Re: problem in reading xml file with SAX parser.

    You have to read the original document from the file, update it with new entries and save it back again. No temporary files are needed.
    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.


  6. #6
    Join Date
    May 2011
    Posts
    120
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    9

    Default Re: problem in reading xml file with SAX parser.

    Can you please give me some sample code. I have confused a lot.

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

    Default Re: problem in reading xml file with SAX parser.

    Qt Code:
    1. QFile file("some.xml");
    2. file.open(QFile::ReadWrite);
    3. doc.setContent(&file);
    4. QDomElement root = doc.documentElement();
    5. QDomElement newOne = root.createElement("elem");
    6. root.appendChild(newOne);
    7. file.seek(0);
    8. QTextStream stream(&file);
    9. doc.save(stream); // or file.write(doc.toByteArray());
    To copy to clipboard, switch view to plain text mode 
    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.


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

    Niamita (14th September 2011)

  9. #8
    Join Date
    May 2011
    Posts
    120
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    9

    Default Re: problem in reading xml file with SAX parser.

    I tried as you suggested but now i am facing a problem when i close the program and then write on the file , it write nothing on the file but it is working fine when the program is running once. I am getting error that "Calling appendChild() on a null node does nothing. " what is wrong with this.

    My code is:

    Qt Code:
    1. const int Indent = 4;
    2. static QDomDocument doc;
    3. static QDomElement root,root1;
    4. QDomElement statement, action_node, condition_node;
    5. root = doc.createElement("root");
    6. QFile *file = new QFile("E:/new.txt");
    7. if(!file->exists())
    8. {
    9. file->open(QIODevice::WriteOnly);
    10. doc.appendChild(root);
    11. }
    12. else
    13. {
    14. file->open(QFile::ReadWrite);
    15. root1 = doc.documentElement();
    16. statement = doc.createElement("statement");
    17. condition_node = doc.createElement("condition");
    18. action_node = doc.createElement("action");
    19. QDomText condition_text = doc.createTextNode(condition_text_edit->toPlainText());
    20. QDomText action_text = doc.createTextNode(action_text_edit->toPlainText());
    21. statement.appendChild(condition_node);
    22. statement.appendChild(action_node);
    23. condition_node.appendChild(condition_text);
    24. action_node.appendChild(action_text);
    25. root1.appendChild(statement);
    26. file->seek(0);
    27. }
    28. QTextStream out(file);
    29. doc.save(out, Indent)
    To copy to clipboard, switch view to plain text mode 

    It is solved. Actually i am not setting contents of document.
    doc.setContent(file);
    Last edited by Niamita; 14th September 2011 at 07:08.

  10. #9
    Join Date
    May 2011
    Posts
    120
    Qt products
    Qt3 Qt4
    Platforms
    Windows
    Thanks
    9

    Default Re: problem in reading xml file with SAX parser.

    Hi

    How to update a textnode data in xml through QDomElement.
    I have following code

    Qt Code:
    1. static QDomDocument doc;
    2. static QDomElement root,root1;
    3. QDomElement statement, action_node, condition_node;
    4. QDomText id_text;
    5. root = doc.createElement("root");
    6. QFile *file = new QFile("E:/new.txt");
    7. if(!file->exists())
    8. {
    9. file->open(QIODevice::WriteOnly);
    10. QDomElement id = doc.createElement("id");
    11. id_text = doc.createTextNode("0");
    12. root.appendChild(id);
    13. id.appendChild(id_text);
    14. doc.appendChild(root);
    15. }
    16. else
    17. {
    18. file->open(QFile::ReadWrite);
    19. doc.setContent(file);
    20. root1 = doc.documentElement();
    21. QDomElement node = root1.firstChildElement("id");
    22. QString string_data = node.text();
    23. int a = string_data.toInt();
    24. qDebug()<<string_data;
    25. string_data.clear();
    26. id_text = doc.createTextNode("Qt"); // Here i want to write Qt but it is getting write 0Qt on the textnode.
    27. node.appendChild(id_text);
    28. statement = doc.createElement("statement");
    29. condition_node = doc.createElement("condition");
    30. action_node = doc.createElement("action");
    31. QDomText condition_text = doc.createTextNode(condition_text_edit->toPlainText());
    32. QDomText action_text = doc.createTextNode(action_text_edit->toPlainText());
    33. statement.setAttribute("id", i);
    34. statement.appendChild(condition_node);
    35. statement.appendChild(action_node);
    36. condition_node.appendChild(condition_text);
    37. action_node.appendChild(action_text);
    38. root1.appendChild(statement);
    39.  
    40. // QString str = statement.attribute("id");
    41. // qDebug()<<str.toInt();
    42. file->seek(0);
    43.  
    44. }
    45. QTextStream out(file);
    46. doc.save(out, Indent);
    To copy to clipboard, switch view to plain text mode 
    So i want to update the textnode value while it is getting append in previous value.
    Please anyone help me.
    Waiting for response.

Similar Threads

  1. Which parser is better for edit xml file.
    By Niamita in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2011, 08:27
  2. Problem: Reading and editing text file data
    By dipeshtech in forum Newbie
    Replies: 2
    Last Post: 3rd May 2011, 00:47
  3. XML parser problem
    By Cantora in forum Newbie
    Replies: 9
    Last Post: 5th June 2009, 13:13
  4. Problem with reading from file
    By iamjayanth in forum Qt Programming
    Replies: 15
    Last Post: 23rd April 2009, 14:12
  5. Problem with reading a file
    By Buhmann in forum Qt Programming
    Replies: 11
    Last Post: 17th February 2006, 14:02

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
  •  
Qt is a trademark of The Qt Company.