the file is loaded in memory and changes are done in memory... to save the changes back in file just open the same file with writeonly(to erase old contents) and then save
void QDomNode::save ( QTextStream & str, int indent ) const
the file is loaded in memory and changes are done in memory... to save the changes back in file just open the same file with writeonly(to erase old contents) and then save
void QDomNode::save ( QTextStream & str, int indent ) const
That's principal right, but I guess you want to read the value somewhere else in your program. So every time open, save, read in elsewhere is not very good. Therefore only create one instance and pass a pointer of the QDomDocument to the needed classes, or use a singleton approach.
ya.. i was just explaning the process.. ofcourse the read n write should be once.
Thanks. The following code does update the xml file now. However, it duplicates the content.
QFile file;
file.setFileName("autocal.xml");
file.open(QIODevice::ReadWrite | QIODevice::Text);
QDomDocument doc("autocal");
doc.setContent(file);
//do some reading here
QDomNodeList list = doc.elementsByTagName("calibrationOptions");
QDomNode parent = list.at(0);
QDomNode n = parent.firstChild();
if (n.isElement() && n.nodeName()=="camera")
{
QDomElement e = n.toElement();
e.setAttribute("value", "1");
}
QTextStream stream(file);
doc.save(stream, 4);
file.close();
--------------------------------------------------------------------------------------
Do I have to open the file twice like below (first time readonly, second time writeonly)?
QFile file;
file.setFileName("autocal.xml");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QDomDocument doc("autocal");
doc.setContent(file);
file.close();
//do some reading here
QDomNodeList list = doc.elementsByTagName("calibrationOptions");
QDomNode parent = list.at(0);
QDomNode n = parent.firstChild();
if (n.isElement() && n.nodeName()=="camera")
{
QDomElement e = n.toElement();
e.setAttribute("value", "1");
}
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream stream(file);
doc.save(stream, 4);
file.close();
No you can also use QIODevice::setOpenMode().
what happened?? is it working or not?
Bookmarks