#include#include #include int main() { QByteArray xml; QXmlStreamWriter writer(&xml); writer.setAutoFormatting(true); writer.writeStartDocument(); writer.writeStartElement("root"); writer.writeAttribute("version", "1.0"); writer.writeTextElement("name", "John"); writer.writeEndElement(); writer.writeEndDocument(); QString result = QString::fromUtf8(xml); qDebug() << result; return 0; }
#includeIn this example, we create an XML file with a root element and one child element with a text content. We use the QXmlStreamWriter class to write the XML content to a file. QXmlStreamWriter is part of the Qt Core module of the Qt framework.#include int main() { QFile file("test.xml"); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return 1; QXmlStreamWriter writer(&file); writer.setAutoFormatting(true); writer.writeStartDocument(); writer.writeStartElement("root"); writer.writeTextElement("name", "Jane"); writer.writeEndElement(); writer.writeEndDocument(); file.close(); return 0; }