【问题标题】:QXmlStreamWriter find tag value and replace with another specific valueQXmlStreamWriter 查找标记值并替换为另一个特定值
【发布时间】:2020-01-13 16:50:15
【问题描述】:

我有一个 xml 文件,其中包含英语和法语字符串作为消息。我正在尝试从 xml 文件中读取特定元素并将其值替换为另一个特定值。

示例(在下面的 xml 文件中):将“Bonjour le monde”替换为“ bonjour le monde à nouveau”。

知道如何使用 QXmlStreamReader 和 QXmlStreamWriter 来实现吗? 我的示例程序无法正常工作。我正在使用 qt 5.14.0

//xml 文件: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>

【问题讨论】:

  • 显示你的示例程序
  • @Deep 老实说,我没有示例程序。你有什么建议吗?

标签: xml qt qxmlstreamreader


【解决方案1】:

答案:-->

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

【讨论】:

  • 答案是不是有点误导,因为问题是关于使用 QXmlStreamReader 和 QXmlStreamWriter 来编辑一个值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2016-09-16
  • 2015-11-27
  • 2022-11-20
  • 2017-11-27
  • 2020-11-05
  • 2017-01-22
相关资源
最近更新 更多