Results 1 to 6 of 6

Thread: Problem in editing xml file through Dom parser.

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

    Default Problem in editing xml file through Dom parser.

    Hi all

    i am in a trouble. My xml file is not edit right. Whem i am editing the file the contents to be edit are written in file with the rest data of file again. I am using Dom for reading and writing xml.

    My code is

    Qt Code:
    1. void HomeWindow::create_xml_file()
    2. {
    3. QFile *file = new QFile("E:/new.txt");
    4. if(!edit_flag){ /// craeting the file and write in it in this condition
    5. const int Indent = 4;
    6. int id_value;
    7. static QDomDocument doc;
    8. static QDomElement root,root1;
    9. QDomElement statement, action_node, condition_node;
    10. QDomText id_text;
    11. root = doc.createElement("root");
    12.  
    13. if(!file->exists())
    14. {
    15. file->open(QIODevice::WriteOnly);
    16. QDomElement id = doc.createElement("id");
    17. id_text = doc.createTextNode("0");
    18. root.appendChild(id);
    19. id.appendChild(id_text);
    20. doc.appendChild(root);
    21. }
    22. else
    23. {
    24. file->open(QFile::ReadWrite);
    25. doc.setContent(file);
    26. root1 = doc.documentElement();
    27. QDomElement node = root1.firstChildElement("id");
    28. QString id_string = node.text();
    29. id_value = id_string.toInt();
    30. QDomElement new_id_node = doc.createElement(QString("id"));
    31. QDomText new_id_Text = doc.createTextNode(QString::number(++id_value));
    32. new_id_node.appendChild(new_id_Text); // replace existing node with new node
    33. root1.replaceChild(new_id_node, node);
    34. statement = doc.createElement("statement");
    35. condition_node = doc.createElement("condition");
    36. action_node = doc.createElement("action");
    37. QDomText condition_text = doc.createTextNode(condition_text_edit->toPlainText());
    38. QDomText action_text = doc.createTextNode(action_text_edit->toPlainText());
    39. statement.setAttribute("id", id_value);
    40. statement.appendChild(condition_node);
    41. statement.appendChild(action_node);
    42. condition_node.appendChild(condition_text);
    43. action_node.appendChild(action_text);
    44. root1.appendChild(statement);
    45. file->seek(0);
    46. static int i =1;
    47. table->setRowCount(i);
    48. QTableWidgetItem *item_id = new QTableWidgetItem(QString::number(id_value));
    49. QTableWidgetItem *item_condition = new QTableWidgetItem(condition_text_edit->toPlainText());
    50. QTableWidgetItem *item_action = new QTableWidgetItem(action_text_edit->toPlainText());
    51. int row = i;
    52. row = --row;
    53. table->setItem(row, 0, item_id);
    54. table->setItem(row, 1, item_condition);
    55. table->setItem(row, 2, item_action);
    56. i++;
    57. condition_text_edit->clear();
    58. action_text_edit->clear();
    59. }
    60. QTextStream out(file);
    61. doc.save(out, Indent);
    62. if(file->error())
    63. qDebug()<<"error"<<file->errorString();
    64. }
    65. else{ ///////edit the file
    66. const int indent =8;
    67. file->open(QFile::ReadWrite);
    68. doc.setContent(file);
    69. QDomElement root_edit = doc.documentElement();
    70. QDomNode node = root_edit.firstChild();
    71. while(!node.isNull())
    72. {
    73. if (node.toElement().tagName() == "statement"){
    74. if(string_id == node.toElement().attribute("id"))
    75. {
    76. QDomElement statement = doc.createElement("statement");
    77. statement.setAttribute("id", string_id);
    78. QDomElement condition_node = doc.createElement("condition");
    79. QDomElement action_node = doc.createElement("action");
    80. QDomText condition_text = doc.createTextNode(condition_text_edit->toPlainText());
    81. QDomText action_text = doc.createTextNode(action_text_edit->toPlainText());
    82. statement.appendChild(condition_node);
    83. statement.appendChild(action_node);
    84. condition_node.appendChild(condition_text);
    85. action_node.appendChild(action_text);
    86. root_edit.replaceChild(statement, node.toElement());
    87. break;
    88. }
    89. }
    90. node = node.nextSibling();
    91. }
    92. QTextStream out(file);
    93. doc.save(out, indent);
    94. QTableWidgetItem *item_id = new QTableWidgetItem(string_id);
    95. QTableWidgetItem *item_condition = new QTableWidgetItem(condition_text_edit->toPlainText());
    96. QTableWidgetItem *item_action = new QTableWidgetItem(action_text_edit->toPlainText());
    97. table->setItem(row_index, 0, item_id);
    98. table->setItem(row_index, 1, item_condition);
    99. table->setItem(row_index, 2, item_action);
    100. condition_text_edit->clear();
    101. action_text_edit->clear();
    102. edit_flag = false;
    103. }
    104. }
    To copy to clipboard, switch view to plain text mode 

    My xml file is looking like following

    <root>
    <id>3</id>
    <statement id="1">
    <condition>Digital Input Equals to END</condition>
    <action>Analog Output Equals to END</action>
    </statement>
    <statement id="2">
    <condition>Digital Input Equals to END</condition>
    <action>Analog Output Equals to END</action>
    </statement>
    <statement id="3">
    <condition>Digital Input Equals to END</condition>
    <action>Analog Output Equals to END</action>
    </statement>
    </root>
    tput Equals to END</action>
    </statement>
    <statement id="2">
    <condition>Digital Input Equals to END</condition>
    <action>gffjryjuuyd</action>
    </statement>
    </root>
    <root>
    <id>3</id>
    <statement id="1">
    <condition>Digital Input Equals to END</condition>
    <action>Analog Output Equals to END</action>
    </statement>
    <statement id="2">
    <condition>Digital Input Equals to END</condition>
    <action>Analog Output Equals to END</action>
    </statement>
    <statement id="3">
    <condition>Digital Input Equals to END</condition>
    <action>gjkghk</action>
    </statement>
    </root>


    Please guide me where i am wrong.
    Waiting for quick reply.
    Thanks.

  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: Problem in editing xml file through Dom parser.

    If a file is 10 bytes long and you seek to the beginning and write 8 bytes, the file will remain to be 10 bytes long -- you will only modify the first 8 bytes, leaving the last two unchanged. If you want to discard the remaining bytes, you need to truncate the file.
    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. #3
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in editing xml file through Dom parser.

    but i did not seek the file at the time of editing. Have you look at the xml file which generate after editing it.
    Please give me some sample code.
    Thanks.

  4. #4
    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: Problem in editing xml file through Dom parser.

    I have already said everything I had to say, you have all info you need, just make proper use of it -- this is not kindergarden. Analyze your own code and reach your own conclusions. I can only repeat -- if you want to have the file be shorter than it was before, you need to truncate the file. This is general programming knowledge, nothing related to Qt or even C++.
    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.


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

    Default Re: Problem in editing xml file through Dom parser.

    but i did not seek the file at the time of editing.
    Sure you did...
    Qt Code:
    1. root1.appendChild(statement);
    2. file->seek(0);
    3. static int i =1;
    4. table->setRowCount(i);
    To copy to clipboard, switch view to plain text mode 
    The whole approach needs revision IMHO. Assuming the XML file is fairly small, the process is:
    • Read the whole document in the DOM from disc or create an "empty" DOM document if no file exists yet
    • Create/modify the document in the DOM using the relevant methods and whatever UI
    • Write the whole DOM document back to disc overwriting the old file if one exists.

    No fiddling about with read/write files or trying to update in-situ. You should be able to draw solid dividing lines between the three elements.
    Last edited by ChrisW67; 20th September 2011 at 09:16.

  6. #6
    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: Problem in editing xml file through Dom parser.

    ...and truncate the file if you intend to write less than the previous content of the file.
    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.


Similar Threads

  1. problem in reading xml file with SAX parser.
    By Niamita in forum Qt Programming
    Replies: 11
    Last Post: 14th September 2011, 10:13
  2. Which parser is better for edit xml file.
    By Niamita in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2011, 07:27
  3. Problem: Reading and editing text file data
    By dipeshtech in forum Newbie
    Replies: 2
    Last Post: 2nd May 2011, 23:47
  4. Editing a file in Qt4
    By grsandeep85 in forum Qt Programming
    Replies: 3
    Last Post: 22nd December 2009, 09:33
  5. XML parser problem
    By Cantora in forum Newbie
    Replies: 9
    Last Post: 5th June 2009, 12:13

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.