Append nodes to an XML file
-
Try to pass a proper xml - it should start with '<?xml version="1.0"?>'
-
I tried with a modified xml but the same issue.
This error comes only when I open the file to
Append
:QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text
But if it's just for
ReadWrite
QIODevice::ReadWrite | QIODevice::Text
Atleast file is leading. Though I can't change the xml file..
-
Did not notice it - for sure QIODevice::Append will not work. So what's your actual output?
-
If I do
QIODevice::ReadWrite | QIODevice::Text
, file loading ok. But I cant write new nodes to it..Consol says
ReadOnly device
Any idea where I'm wrong?
-
@russjohn834
If I understand what you are trying to do correctly, we have gone through this many times in this forum. If your goal is to update XML in a file, you don't open the file for append or read-write or anything like that. You open the file read-only, read it all in, close it. Make your changes to the in-memory document. Then open the file for overwrite, write all content, close file. (Or write to a new file and then delete/rename.) Your code will contain neitherQIODevice::ReadWrite
norQIODevice::Append
. -
@russjohn834 said in Append nodes to an XML file:
Hi
......
root.appendChild(newTag);
there is missing a file open here ? ( you call close higher up but i dont see an open for write ?)
QTextStream output(&file);
output << document.toString();
file.close(); -
This post is deleted!
-
Thank you @mrjj @JonB
I can now append a new node to my file, I did as follows:if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug () << "Error saving XML file...."; file.close(); } document.setContent(&file); QDomElement root = document.documentElement(); file.close(); QDomElement newTag = document.createElement(QString("Settings")); QDomElement newCurrTag = document.createElement(QString("Current")); QDomText newCurrVal = document.createTextNode(QString("45")); newCurrTag.appendChild(newCurrVal); newTag.appendChild(newCurrTag); root.appendChild(newTag); file.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream output(&file); output << document.toString(); file.close();
-
@russjohn834
That's much better!Now just a minor picky for good code.
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug () << "Error saving XML file...."; file.close(); } ...
Don't forget to put some kind of
return
at the end of that error case so it does not continue into the "opened" code. And (unless you know better forQFile
) don'tfile.close()
if you are inside anif (!file.open())
, because the file never got opened! Plus, change all that indentation after theif
's}
, so it doesn't look like it's all still inside theif
:) -
@JonB Thank you for the feedback :)