QXmlStreamWriter 查找标签值并替换为另一个特定值
QXmlStreamWriter find tag value and replace with another specific value
我有一个 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>
答案:-->
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();
}
}
}
我有一个 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>
答案:-->
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();
}
}
}