轉(zhuǎn)自QT4.86寫中文XML
新建Qt空項(xiàng)目名稱xml.pro
xml.pro
QT += core gui xml
SOURCES += \
main.cpp
main.cpp
#include <QtGui/QApplication>
#include <QTextCodec>
#include <QFile>
#include <QXmlStreamWriter>
int main()
{
//QTextCodec::setCodecForCStrings(QTextCodec::codecForName("gb18030"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
QFile file("test.xml");
if(! file.open(QIODevice::ReadWrite))
return 0;
QXmlStreamWriter writer(&file);
writer.setAutoFormatting(true);
// 作用類似于創(chuàng)建一個(gè)xml文檔枝冀,并在文檔開(kāi)頭部分寫入版本信息和編碼信息遂填。
writer.writeStartDocument();//生成的文件開(kāi)頭標(biāo)識(shí)仍是utf-8
//writer.setCodec("gb18030");
writer.setCodec("UTF-8");
writer.writeStartElement("Start");
writer.writeAttribute("animinal","cat");
writer.writeTextElement("book","book1");
writer.writeTextElement("book","中國(guó)");
writer.writeEndElement();//Start
writer.writeEndDocument();
file.close();
return 0;
}