PDA

View Full Version : Reading XML file using DOM



rk0747
3rd February 2010, 17:44
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.

pitonyak
3rd February 2010, 19:13
This demonstrates how to open an XML file as a DOM document.

QString filename = "C:/test.xml";
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
qDebug(qPrintable(QString("Error: Cannot read file %1 %2").arg(filename).arg(file.errorString())));
return;
}
QDomDocument doc;
doc.setContent(&file);
file.close();

rk0747
4th February 2010, 03:36
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.

aamer4yu
4th February 2010, 04:42
Did you read about the QLineEdit class ? how to set and get text ?

pitonyak
4th February 2010, 05:50
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:


How can I read XML from a file (as in an entire file), and place the contents into a line edit control.
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:

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

rk0747
4th February 2010, 17:47
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?

faldzip
4th February 2010, 22:09
You have to read a file and set it contents to dom document, for example:


QFile file("some.xml");
file.open(QFile::ReadOnly|QFile::Text);
QDomDocument doc;
if (!doc.setContent(file.readAll())) {
qDebug("Parsing failed!");
return;
}