#include <QtGui/QApplication>
#include <QDomDocument>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDomDocument doc;
QDomNode instruction = doc.createProcessingInstruction("xml","version = \"1.0\" encoding = \" UTF-8\"");
doc.appendChild(instruction);
QDomElement root = doc.createElement("Notes");
doc.appendChild(root);
QDomElement note = doc.createElement("note");
root.appendChild(note);
QDomElement no = doc.createElement("no");
note.appendChild(no);
QDomText no_text = doc.createTextNode("001");
no.appendChild(no_text);
QFile file("test.xml");
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate |QIODevice::Text))
return 1 ;
QTextStream out(&file);
out.setCodec("UTF-8");
doc.save(out,4,QDomNode::EncodingFromTextStream);
file.close();
return a.exec();
}
运行结果:
<?xml version="1.0" encoding="UTF-8" ?>