QXmlStreamReader reader(xmlData); while (!reader.atEnd()) { QXmlStreamReader::TokenType token = reader.readNext(); if (token == QXmlStreamReader::StartElement) { if (reader.name() == "book") { QXmlStreamAttributes attributes = reader.attributes(); QString title = attributes.value("title").toString(); QString author = attributes.value("author").toString(); int year = attributes.value("year").toInt(); // do something with the attribute values } } }
QXmlStreamWriter writer(&output); writer.setAutoFormatting(true); writer.writeStartElement("book"); writer.writeAttribute("title", "The Great Gatsby"); writer.writeAttribute("author", "F. Scott Fitzgerald"); writer.writeAttribute("year", "1925"); writer.writeEndElement();In this code, we create a QXmlStreamWriter object and write an element with attributes using the writeStartElement and writeAttribute methods. We provide QXmlStreamAttributes with the attribute values for the "title", "author", and "year" attributes. QXmlStreamAttributes is a part of the Qt Core library.