QXmlStreamWriter find tag value and replace with another specific value

484 Views Asked by At

I have a xml files which contains English and French strings as messages. I am trying to read a specific element from xml file and replace their value with some another specifc value.

Example (In the below xml file): Replace "Bonjour le monde" with " bonjour le monde à nouveau".

Any idea how to achieve this using QXmlStreamReader and QXmlStreamWriter? My sample program is not working properlty. I am using qt 5.14.0

//xml file : myfile.xml

<?xml version='1.0' encoding='utf-8'?>
<TS language="fr_FR" version="2.1">
<context>
  <name>TRStringFactory</name>
  <message>
      <location filename="test.cpp" line="28" />
      <source>none</source>
      <translation type="unfinished">aucun</translation>
  </message>
  <message>
      <location filename="test.cpp" line="29" />
      <source>hello world</source>
      <translation type="unfinished">Bonjour le monde</translation>
  </message>
</context>
</TS>
1

There are 1 best solutions below

1
RaviS Gupta On BEST ANSWER

Answer :-->

void updateXMLFile(QString fileName)
{
if (fileName != "")
{
QDomDocument document;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
    return;
if (!document.setContent(&file))
{
    file.close();
    return;
}
file.close();

QString in = "aucun";
QString out = "ravi gupta";

QDomElement root = document.documentElement();
QString name = root.tagName();
qDebug()<<"tag name"<<name;
QDomNode fChild = root.firstChild();
while(!fChild.isNull())
{
    QDomElement fElement = fChild.toElement();
    if(!fElement.isNull())
    {
        qDebug()<<"fElement.tagName() : " <<fElement.tagName();

        if(fElement.tagName() == "context" )//Some Dummy tagname)
        {
            QDomNode enabledChecks = fChild.firstChild();
            while(!enabledChecks.isNull())
            {

                QDomElement checkName = enabledChecks.toElement();
                //Some Code

                QDomNode sChild = enabledChecks.firstChild();
                while (!sChild.isNull())
                {
                    QDomElement x = sChild.toElement();

                    qDebug()<<"second child : " <<x.tagName()<<x.text();

                    if(x.text() == in)
                    {
                        // create a new node with a QDomText child
                        QDomElement newNodeTag = document.createElement(QString("translation"));
                        QDomText newNodeText = document.createTextNode(out);
                        newNodeTag.appendChild(newNodeText);

                        checkName.replaceChild(newNodeTag,x);
                        //sChild.removeChild(x);

                        qDebug()<<"matched";
                        // Save content back to the file
                        if (!file.open(QIODevice::Truncate | QIODevice::WriteOnly)) {
                            qDebug("Basically, now we lost content of a file");
                            return;
                        }

                        file.resize(0);
                        QTextStream stream;
                        stream.setDevice(&file);
                        document.save(stream, 4);

                        file.close();
                    }

                    sChild = sChild.nextSibling();
                }


                enabledChecks = enabledChecks.nextSibling();
            }
        }

    }
    fChild = fChild.nextSibling();
}
}
}