我不知道你的为什么不行,也不知道你想要添加怎样的节点
这是我以前写的代码,运行正常:
int DevModel::save(const QString & fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
qDebug("cannot create file!");
return -1;
}
QDomDocument doc;
QDomElement emt;
QDomProcessingInstruction dpi;
dpi = doc.createProcessingInstruction("xml", "version=\"1.0\"");
doc.appendChild(dpi);
QDomElement root = doc.createElement("config");
doc.appendChild(root);
foreach(DEV_INFO devInfo, d->devInfos)
{
QDomElement child = doc.createElement("dev");
child.setAttribute("no", devInfo.no);
child.setAttribute("name", devInfo.name);
child.setAttribute("desc", devInfo.desc);
child.setAttribute("fn", devInfo.fn);
root.appendChild(child);
}
QTextStream out(&file);
doc.save(out, 4);
QMessageBox::information(0, "", tr("保存成功"));
return 0;
}
// DEV_INFO 是我自己定义的结构体