Sha*_*adi 6 xml qt xml-parsing
我有Xml文件
<root rootname="RName" otherstuff="temp">
<somechild childname="CName" otherstuff="temp">
</somechild>
</root>
Run Code Online (Sandbox Code Playgroud)
在上面的XML我怎么可以更新RName到RN和CName到CN使用QT.我正在使用QDomDocument但不能做必需的事情.
div*_*nov 14
如果您分享信息如何使用QDomDocument以及哪个部分确实很棘手,这将有所帮助.但是这里一般情况如何:
正在从文件系统中读取文件;
文件正被解析为QDomDocument;
正在修改文件内容;
数据正被保存回文件.
在Qt代码中:
// Open file
QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly)) {
qError("Cannot open the file");
return;
}
// Parse file
if (!doc.setContent(&file)) {
qError("Cannot parse the content");
file.close();
return;
}
file.close();
// Modify content
QDomNodeList roots = elementsByTagName("root");
if (roots.size() < 1) {
qError("Cannot find root");
return;
}
QDomElement root = roots.at(0).toElement();
root.setAttribute("rootname", "RN");
// Then do the same thing for somechild
...
// Save content back to the file
if (!file.open(QIODevice::Truncate | QIODevice::WriteOnly)) {
qError("Basically, now we lost content of a file");
return;
}
QByteArray xml = doc.toByteArray();
file.write(xml);
file.close();
Run Code Online (Sandbox Code Playgroud)
请注意,在实际应用程序中,您需要将数据保存到另一个文件,确保保存成功,然后用副本替换原始文件.
| 归档时间: |
|
| 查看次数: |
6964 次 |
| 最近记录: |