Results 1 to 12 of 12

Thread: problem in reading xml file with SAX parser.

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

    Default problem in reading xml file with SAX parser.

    Hi all

    I am reading a xml file but i am not successing in reading it. My code for reading file is

    Qt Code:
    1. Xmlhandler::Xmlhandler(QTableWidget *table)
    2. {
    3. tablewidget = table;
    4. table_item = 0;
    5. }
    6.  
    7. bool Xmlhandler::startElement(const QString &, const QString &, const QString &qName,const QXmlAttributes &attributes)
    8. {
    9. if (qName == "statement"){
    10. tablewidget->insertRow(1);
    11.  
    12. if(qName == "Condition")
    13. {
    14. QString str_id = attributes.value("text");
    15. qDebug()<<str_id;
    16. table_item = new QTableWidgetItem();
    17.  
    18. }
    19. else if(qName == "action")
    20. {
    21. QString str_id = attributes.value("text");
    22. qDebug()<<str_id;
    23. table_item = new QTableWidgetItem();
    24.  
    25. }
    26. else if(qName == "text")
    27. currenttext.clear();
    28. }
    29. return true;
    30. }
    31.  
    32. bool Xmlhandler::characters(const QString &str)
    33. {
    34. currenttext += str;
    35. return true;
    36. }
    37.  
    38. bool Xmlhandler::endElement(const QString &,const QString &,const QString &qName)
    39. {
    40. if(qName == "statement"){
    41. if (qName == "Condition") {
    42. if (qName == "page") {
    43. table_item->setText(currenttext);
    44. tablewidget->setItem(0, 0, table_item);
    45. }
    46. }
    47. if (qName == "action") {
    48. if (qName == "page") {
    49. table_item->setText(currenttext);
    50. tablewidget->setItem(0, 1, table_item);
    51. }
    52. }
    53. }
    54. return true;
    55. }
    56.  
    57. bool Xmlhandler::fatalError(const QXmlParseException &exception)
    58. {
    59. QMessageBox::warning(0, QObject::tr("SAX Handler"),QObject::tr("Parse error at line %1, column "
    60. "%2:\n%3.").arg(exception.lineNumber()).arg(exception.columnNumber()).arg(exception.message()));
    61. return false;
    62. }
    To copy to clipboard, switch view to plain text mode 

    and my xml file is like this-

    <?xml version="1.0" encoding="UTF-8"?>
    <statement>
    <Condition id="1">
    <text></text>
    </Condition>
    <action id="1">
    <text></text>
    </action>
    </statement>

    <statement>
    <Condition id="2">
    <text>Analog Input Equals to 1 END</text>
    </Condition>
    <action id="2">
    <text>Digital Output Equals to ON END</text>
    </action>
    </statement>

    i am trying to read xml and display it in a table.

    Please guide me , i am doing this since three days but i am not getting desired result.

    Thank you.

  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 reading xml file with SAX parser.

    What is wrong exactly? Saying "it doesn't work" doesn't provide any hints as to what is wrong. One thing might be that you don't have a root element in your xml.
    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 reading xml file with SAX parser.

    ya i also think so , so that i am now generating xml with rrot element through DOM parser, but when i create file then it is ok but when i want to add new entries it is not getting append to the root node in xml file.
    My code for this is

    Qt Code:
    1. const int Indent = 4;
    2. static QDomDocument doc;
    3. static QDomElement root;
    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(QIODevice::Append);
    15. statement = doc.createElement("statement");
    16. condition_node = doc.createElement("condition");
    17. action_node = doc.createElement("action");
    18. QDomText condition_text = doc.createTextNode(condition_text_edit->toPlainText());
    19. QDomText action_text = doc.createTextNode(action_text_edit->toPlainText());
    20. root.appendChild(statement);
    21. statement.appendChild(condition_node);
    22. statement.appendChild(action_node);
    23. condition_node.appendChild(condition_text);
    24. action_node.appendChild(action_text);
    25. }
    26. if(file->error())
    27. qDebug()<<file->errorString();
    28.  
    29. QTextStream out(file);
    30. doc.save(out, Indent);
    To copy to clipboard, switch view to plain text mode 

    what i am doing wrong now.
    Last edited by wysota; 13th September 2011 at 12:41. Reason: missing [code] tags

  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 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.


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

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

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

  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 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.


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

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

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

  8. #8
    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 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.


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

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

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

  10. #10
    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 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.


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

    Niamita (14th September 2011)

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

    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 06:08.

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

    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, 07:27
  2. Problem: Reading and editing text file data
    By dipeshtech in forum Newbie
    Replies: 2
    Last Post: 2nd May 2011, 23:47
  3. XML parser problem
    By Cantora in forum Newbie
    Replies: 9
    Last Post: 5th June 2009, 12:13
  4. Problem with reading from file
    By iamjayanth in forum Qt Programming
    Replies: 15
    Last Post: 23rd April 2009, 13:12
  5. Problem with reading a file
    By Buhmann in forum Qt Programming
    Replies: 11
    Last Post: 17th February 2006, 13: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.