Search code examples
xmlqtqdomdocument

Remove line from XML with QDomDocument


Im working with XML files in Qt, and i need to copy an XML file and then append to the copy. One issue im having is i need to delete the very last line of the XML file which is:

</VSCommands>

I am very new to working with XMLs in Qt and using QDom, so im here asking for help on what is the best way to approach copying the xml file, and deleting that last line, then appending my new data to the end of the file?

Edit: I have copied the original xml to a new xml using:

QFile readFile(filename);
readFile.copy(new_filename);
QFile writeFile(new_filename);

And now i have opened it to append onto the file with:

if (writeFile.open(QIODevice::Append))
{

}

I am just unsure of how to delete the last line of the file first, before i begin to append the new data.


Solution

  • Once you read the XML file into a DOM, you have an abstract representation of it. You do the changes on the DOM, the write the DOM out to a new file, or overwrite the old one. Just make sure that you use QSaveFile to do the writing. You don't worry about how to make the individual changes to the xml file.