QString fileName(tr("output.xml")); QFile file(fileName); if (!file.open(QFile::WriteOnly | QFile::Text)) { std::cerr << "Error: Cannot open file" << std::endl; } QString strA(tr("A")),strB(tr("B")),strC(tr("C")),strD(tr("D")); QXmlStreamWriter xmlWriter(&file); xmlWriter.setAutoFormatting(true); xmlWriter.writeStartDocument(); xmlWriter.writeStartElement("result"); xmlWriter.writeStartElement("A"); xmlWriter.writeCharacters(strA); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("B"); xmlWriter.writeCharacters(strB); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("C"); xmlWriter.writeCharacters(strC); xmlWriter.writeEndElement(); xmlWriter.writeStartElement("D"); xmlWriter.writeCharacters(strD); xmlWriter.writeEndElement(); xmlWriter.writeEndElement(); xmlWriter.writeEndDocument(); file.close();原文链接:https://www.f2er.com/xml/295887.html