With QXmlStreamWriter you can only write the whole document, inclusive the chanced value. But if you want to chance singe values without writing the whole document each time you have to use QDomDocument.
With QXmlStreamWriter you can only write the whole document, inclusive the chanced value. But if you want to chance singe values without writing the whole document each time you have to use QDomDocument.
I use the following code to change the value of camera from "0" to "1", but the content of the xml file is not updated at all. Do I miss something?
--------------------------------------------------------------------------------------
QFile file;
file.setFileName("autocal.xml");
file.open(QIODevice::ReadWrite | QIODevice::Text);
QDomDocument doc("autocal");
doc.setContent(file)
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.close();
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