Qt有两种方式可以写xml文件,一是用QxmlStreamWriter。下面看看简单用法
void XmlHandle::writeXmlFileByStreamWriter()
{
QString fileName = "book.xml";
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
return;
}
QXmlStreamWriter writer(&file);
writer.setCodec("UTF-8");
writer.writeStartElement("Root");
writer.writeStartElement("Book");
writer.writeAttribute("Author","John");
writer.writeTextElement("Name","C++ Programming");
writer.writeTextElement("Price","99.5");
writer.writeEndElement();
writer.writeEndElement();
file.close();
}
<?xml version="1.0"?>
-<Root>
-<Book Author="John">
<Name>C++ Programming</Name>
<Price>99.5</Price>
</Book>
</Root>
需要注意的几点ÿ