BobR BobR - 1 month ago 15
C++ Question

QT DOMXml - Change the name of a node

I am working on a QT Project and part of that is a reconstruction of an XML file. I was able to make most of the needed changes with QDom but I can't find how to rename a node.

So the old XML file looks like ..

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


How can i change the XML so that the new one will have < group > instead of < window >?

So at the end it needs to look like..

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


Adding some more info...

Here is the code I use to read the
<window>
nodes, delete some based on the visibility (comes from a list) and I need to change
<window>
to
<group>
for the remaining nodes.

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>
}
}

Answer

You could use setTagName and maybe setAttribute if you want to set a value for the name attribute.

With the following example, myxml.xml is converted to xmlout.xml

  • Note#1: this is just an example: we're replacing only the first node.
  • Note#2: in this example, we're using two different files. Depending on your design, you could use the same or not.

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;
}