qtxmlwriterqtxml

QXmlStreamWriter writes different start of the root element when not writing any text elements


This is code without writing any text elements to the xml file:

QString filename = QFileDialog::getSaveFileName(this, "Vytvorenie databázy", QDir::currentPath(), "XML file(*.xml)");
QFile file(filename);
file.open(QIODevice::WriteOnly);
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.setAutoFormattingIndent(4);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("CARS");
//
//
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
file.close();

This is output:

<?xml version="1.0" encoding="UTF-8"?>
<CARS/>

This is wanted output:

<?xml version="1.0" encoding="UTF-8"?>
<CARS>
</CARS>

Why does it not write start of root element when not writing any additional elements? Is there a way how to explicitly tell writer to write it even when no more elements are added? Same thing happens in C#.


Solution

  • It looks like an inside implementation of QXmlStreamWriter and it does not matter, because both are semantically equivalent and "should be interpreted the same by every parser", as commenter wrote.