QT DOMXml -更改节点的名称

QT DOMXml - Change the name of a node

本文关键字:节点 DOMXml QT      更新时间:2023-10-16

我正在做一个QT项目,其中一部分是一个XML文件的重建。我能够用QDom进行大部分所需的更改,但我找不到如何重命名节点。
所以旧的XML文件看起来像..

<root>
<window name="" ..>
<element x="" y=""/>
<element1 a="" b=""/>
...
</window>
..
..
<window name="">
<element x="" y=""/>
<element1 a="" b=""/>
...
</window>
</root>

我怎么能改变XML,使新的将有<组>而不是<窗口> ?

所以最后它要看起来像。

<root>
    <group name="" ..>
    <element x="" y=""/>
    <element1 a="" b=""/>
    ...
    </group>
    ..
    ..
    <group name="">
    <element x="" y=""/>
    <element1 a="" b=""/>
    ...
    </group>
    </root>

添加更多信息…
以下是我用来读取<window>节点的代码,根据可见性删除一些(来自列表),我需要将剩余节点的<window>更改为<group>

QFile oldXml("file.xml");
    oldXml.open(QIODevice::ReadOnly);
    QDomDocument doc;
    doc.setContent(&oldXml);
    QDomNodeList nodes = doc.elementsByTagName("window");
    // Remove Window nodes based  on visibility
    insize = nodes.length();
    for ( int i = 0; i < insize; i++ ) {
       QDomNode node = nodes.at(i-dels);
       if ( (list2[i] == "0") | (list2[i]=="") )  {
           node.parentNode().removeChild(node);
            dels=dels+1;
       } else {
           // Here is where i need to change the node name from <window> to e.g. <group>
       }
     }

如果您想为name属性设置一个值,您可以使用setTagNamesetAttribute

以下示例将myxml.xml转换为xmlout.xml

    注1:这只是一个例子:我们只替换第一个节点。注2:在这个例子中,我们使用了两个不同的文件。根据你的设计,你可以使用相同的或不。

myxml.xml

<root>
<window name="">
<element x="" y=""/>
<element1 a="" b=""/>
</window>
<window name="">
<element x="" y=""/>
<element1 a="" b=""/>
</window>
</root>

xmlout.xml

<root>
    <group name="value">
        <element y="" x=""/>
        <element1 a="" b=""/>
    </group>
    <window name="">
        <element y="" x=""/>
        <element1 a="" b=""/>
    </window>
</root>

main.cpp

#include <iostream>
#include <QtXml>
#include <QFile>
int main(int argc, char *argv[])
{
    QDomDocument doc;
    // Load xml file as raw data
    QFile inFile(":myxml.xml");
    if (!inFile.open(QIODevice::ReadOnly ))
    {
        std::cerr << "Error - inFile: " << inFile.errorString().toStdString();
        return 1;
    }
    // Set data into the QDomDocument before processing
    doc.setContent(&inFile);
    // Get element in question
    QDomElement root = doc.documentElement();
    QDomElement nodeTag = root.firstChildElement("window");
    nodeTag.setTagName("group");
    nodeTag.setAttribute("name","value");
    inFile.close();
    // Save the modified data
    QFile outFile("xmlout.xml");
    if (!outFile.open(QIODevice::WriteOnly ))
    {
        // Error while loading file
        std::cerr << "Error - outFile: " << outFile.errorString().toStdString();
        return 1;
    }
    QTextStream stream;
    stream.setDevice(&outFile);
    stream.setCodec("UTF-8");
    doc.save(stream,4);
    outFile.close();
    return 0;
}

我没有看到任何直接的API函数来重命名元素。API允许更改值,但不允许更改名称。

还有另一条路。

创建一个元素,例如:

QDomElement group = doc.createElement("group");

使用"QDomNode's replacechild"函数

QDomNode QDomNode::replaceChild(const QDomNode &newChild, const QDomNode &oldChild)

,

QDomNode dNode = node.replaceChild(group,oldNode);