



QDomDocument doc;
QDomElement ss= doc.createElement("root");//创建标签
//ss标签添加到文档对象
doc.appendChild(ss);
//doc.save()
auto hero = doc.createElement("hero");
ss.appendChild(hero);
hero.setAttribute("id",10086);//为hero添加属性
//属性名不能以数字开头
//html中
//<root> <hero id = "10086"><abc>无极剑圣<abc></hero> <abc></root>
//当添加孩子时就变成双标签了
auto name = doc.createElement("abc");
hero.appendChild(name);//hero的子标签
//html 中 <h1>增加内容<h1>
hero.appendChild( doc.createTextNode("无极剑圣"));
// doc.createTextNode("蛮族之王");
QString filename = QFileDialog::getSaveFileName(this,"保存xml");
QFile ff(filename);
if(!ff.open(QIODevice::WriteOnly))
{
QMessageBox::information(NULL,"信息提醒","打开失败");
return;
}
//文本流
QTextStream outstream(&ff);//获取文件的地址 创建文本流对象
doc.save(outstream,4); //第二个参数缩进 4
//默认utf-8 参数可不填
ff.close();//记住文件得关闭
123.xml

读取测试



