Xmlファイルがあります
<root rootname="RName" otherstuff="temp">
     <somechild childname="CName" otherstuff="temp">
     </somechild>
</root>
上記の XML でQTを使用して更新RNameするにRNはどうすればよいですか。使用していますが、必要なことを行うことができません。CNameCNQDomDocument
Xmlファイルがあります
<root rootname="RName" otherstuff="temp">
     <somechild childname="CName" otherstuff="temp">
     </somechild>
</root>
上記の XML でQTを使用して更新RNameするにRNはどうすればよいですか。使用していますが、必要なことを行うことができません。CNameCNQDomDocument
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();
実際のアプリケーションでは、データを別のファイルに保存し、保存が成功したことを確認してから、元のファイルをコピーに置き換える必要があることに注意してください。