You will need to re-write the file with the extra nodes. The stream interface (QXmlStreamReader
/ QXmlStreamWriter
) is more complex to use to do this than the DOM (QDomDocument
) interface, but has the benefit of lower memory requirements.
With the DOM interface you work with an in-memory representation of the XML document. With the stream interface you may need to build and maintain your own representation.
Sample code for the stream interface:
QFile inputFile("in.xml");
if (! inputFile.open(QIODevice::ReadOnly))
// error handling
QFile outputFile("out.xml");
if (! outputFile.open(QIODevice::WriteOnly))
// error handling
QXmlStreamReader inputStream(&inputFile);
QXmlStreamWriter outputStream(&outputFile);
while (! inputStream.atEnd())
{
inputStream.readNext();
// manipulation logic goes here
outputStream.writeCurrentToken(inputStream);
}
Sample code for the DOM interface:
QFile inputFile("in.xml");
if (! inputFile.open(QIODevice::ReadOnly))
// error handling
QDomDocument doc;
if (! doc.setContent(&inputFile))
// error handling
// manipulation logic goes here
QFile outputFile("out.xml");
if (! outputFile.open(QIODevice::WriteOnly))
// error handling
outputFile.write(doc.toByteArray());