Results 1 to 7 of 7

Thread: Reading XML file using DOM

  1. #1
    Join Date
    Jan 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Reading XML file using DOM

    hi,
    i have created an xml file using Dom , i got errors while opening, please can body tell me how to read xml file using Qt. Attached my program file, please find the attachment.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading XML file using DOM

    This demonstrates how to open an XML file as a DOM document.
    Qt Code:
    1. QString filename = "C:/test.xml";
    2. QFile file(filename);
    3. if (!file.open(QFile::ReadOnly | QFile::Text))
    4. {
    5. qDebug(qPrintable(QString("Error: Cannot read file %1 %2").arg(filename).arg(file.errorString())));
    6. return;
    7. }
    8. doc.setContent(&file);
    9. file.close();
    To copy to clipboard, switch view to plain text mode 

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

    rk0747 (4th February 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading XML file using DOM

    hi,
    i didn't ask requirement correctly, i want to read the values from xml file into lineedit in the xmledit.ui. After that i want to edit and update the values from xmledit.ui. please tell me if you know how to do this.

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading XML file using DOM

    Did you read about the QLineEdit class ? how to set and get text ?

  6. #5
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading XML file using DOM

    Quote Originally Posted by rk0747 View Post
    hi,
    i didn't ask requirement correctly, i want to read the values from xml file into lineedit in the xmledit.ui. After that i want to edit and update the values from xmledit.ui. please tell me if you know how to do this.
    My understanding of what you just asked is really two questions:

    1. How can I read XML from a file (as in an entire file), and place the contents into a line edit control.
    2. How can I read the full text from a line edit control and save it in a file.

    Is this what you desire to do, or is it closer to:
    1. How can I read XML from a file. I want to then extract a portion of the file and place this portion into a line edit control.
    2. Later, I want to read the text from the line edit control, and replace a specific portion in the XML

    Even as stated, it is not likely to be sufficient information. For example, if you place XML into this control, is the XML a complete and valid portion of XML? After the user edits the XML, do you check it for valid syntax.

    My point with this is that your question is a bit vague, at least for me. Also, there is a difference between understanding generally what must be done, but missing a step to work the QT library. For example, I understand how DOM works, but I had a bit of trouble figuring out the nuances of the QT implementation (not that I am an expert).

  7. #6
    Join Date
    Jan 2010
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading XML file using DOM

    hi,
    i saved value from lineEdit to an xml file. Again i want to read the values from xml file to lineEdit and then modify(edit ) the values and update it. i used Qdomdocument ,Q domelement and Qdomtext to save the values , but facing dificulty to read the values.



    my xml file is:

    <ECLOGIC>
    <PLCNO> 123</PLCNO>
    <PLCName>motor pump</PLCName>
    </ECLOGIC>

    code to save from lineEdit to a xml file:

    void xmledit::SaveXMLFile()
    {
    filename = QFileDialog::getSaveFileName(this,
    tr("Save Xml"), ".",
    tr("Xml files (*.xml)"));
    QFile file(filename);
    file.open(QIODevice::WriteOnly);

    const int Indent = 4;
    QDomDocument doc;
    QDomElement root = doc.createElement("ECLogic");
    QDomElement Plcwin = doc.createElement("PLCConfig");
    QDomText Plcno = doc.createTextNode(ui->PNumber->text());
    QDomText Plcname = doc.createTextNode(ui->PNumber->text());
    doc.appendChild(root);
    root.appendChild(Plcwin);
    root.appendChild(Plcwin);
    Plcwin.appendChild(Plcno);
    Plcwin.appendChild(Plcname);
    QTextStream out(&file);
    doc.save(out, Indent);
    file.close();


    now how to read the vaues using xmldom?

  8. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading XML file using DOM

    You have to read a file and set it contents to dom document, for example:
    Qt Code:
    1. QFile file("some.xml");
    2. file.open(QFile::ReadOnly|QFile::Text);
    3. if (!doc.setContent(file.readAll())) {
    4. qDebug("Parsing failed!");
    5. return;
    6. }
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. reading a file
    By xaqt in forum Newbie
    Replies: 6
    Last Post: 14th September 2009, 15:04
  2. reading xml file
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2008, 02:01
  3. QFtp reading file from FTP
    By Zergi in forum Newbie
    Replies: 3
    Last Post: 27th March 2008, 14:55
  4. help in reading XML file
    By cshiva_in in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2008, 13:55
  5. reading from a file
    By mickey in forum General Programming
    Replies: 32
    Last Post: 19th July 2007, 01:04

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.