1

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.

Cody Pritchard
  • 635
  • 1
  • 9
  • 28

1 Answers1

1

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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • 1
    @CodyPritchard Actually, given that Qt's XML module is deprecated, you might forget about the DOM, and use the QXmlStreamReader and QXmlStreamWriter classes directly. – Kuba hasn't forgotten Monica Aug 03 '15 at 18:49
  • @CodyPritchard But, if you wish to use the DOM, simply look at `QDomDocument`'s documentation. It has an example of doing exactly what you want to achieve. It's a trivial amount of code, too. – Kuba hasn't forgotten Monica Aug 03 '15 at 18:54