C++ 编辑 QDomElement 的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6753782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Edit Value of a QDomElement?
提问by Eternal Learner
I need to edit the text of a QDomElement - Eg
我需要编辑 QDomElement 的文本 - 例如
I have an XML file with its content as -
我有一个 XML 文件,其内容为 -
<root>
<firstchild>Edit text here</firstchild>
</root>
How do I edit the text of the child element <firstchild>
?
如何编辑子元素的文本<firstchild>
?
I don't see any functions in the QDomElement of QDomDocument classes descriptions provided in Qt 4.7
我在 Qt 4.7 中提供的 QDomDocument 类描述的 QDomElement 中没有看到任何函数
Edit1 - I am adding more details.
Edit1 - 我正在添加更多细节。
I need to read, modify and save an xml file. To format of the file is as below -
我需要读取、修改和保存一个 xml 文件。文件格式如下 -
<root>
<firstchild>Edit text here</firstchild>
</root>
The value of element needs to be edited.I code to read the xml file is -
element的值需要修改。我读取xml文件的代码是-
QFile xmlFile(".\iWantToEdit.xml"); xmlFile.open(QIODevice::ReadWrite); QByteArray xmlData(xmlFile.readAll()); QDomDocument doc; doc.setContent(xmlData);
// Read necessary values
// write back modified values?
QFile xmlFile(".\iWantToEdit.xml"); xmlFile.open(QIODevice::ReadWrite); QByteArray xmlData(xmlFile.readAll()); QDomDocument doc; doc.setContent(xmlData);
// 读取必要的值
// 写回修改后的值?
Note: I have tried to cast a QDomElement to QDomNode and use the function setNodeValue(). It however is not applicable to QDomElement.
注意:我尝试将 QDomElement 转换为 QDomNode 并使用函数 setNodeValue()。但是它不适用于 QDomElement。
Any suggestions, code samples, links would we greatly welcome.
我们非常欢迎任何建议、代码示例、链接。
回答by Lucky Luke
This will do what you want (the code you posted will stay as is):
这将执行您想要的操作(您发布的代码将保持原样):
// Get element in question
QDomElement root = doc.documentElement();
QDomElement nodeTag = root.firstChildElement("firstchild");
// create a new node with a QDomText child
QDomElement newNodeTag = doc.createElement(QString("firstchild"));
QDomText newNodeText = doc.createTextNode(QString("New Text"));
newNodeTag.appendChild(newNodeText);
// replace existing node with new node
root.replaceChild(newNodeTag, nodeTag);
// Write changes to same file
xmlFile.resize(0);
QTextStream stream;
stream.setDevice(&xmlFile);
doc.save(stream, 4);
xmlFile.close();
... and you are all set. You could of course write to a different file as well. In this example I just truncated the existing file and overwrote it.
......你都准备好了。您当然也可以写入不同的文件。在这个例子中,我只是截断了现有文件并覆盖了它。
回答by Petar
Just to update this with better and simpler solution (similar like Lol4t0 wrote) when you want to change the text inside the node. The text inside the 'firstchild' node actually becomes a text node, so what you want to do is:
当您想更改节点内的文本时,只是为了使用更好更简单的解决方案(类似于 Lol4t0 所写)更新它。'firstchild' 节点内的文本实际上变成了一个文本节点,所以你要做的是:
...
QDomDocument doc;
doc.setContent(xmlData);
doc.firstChildElement("firstchild").firstChild().setNodeValue(??"new text");
notice the extra firstChild() call which will actually access the text node and enable you to change the value. This is much simpler and surely faster and less invasive than creating new node and replacing the whole node.
注意额外的 firstChild() 调用,它将实际访问文本节点并使您能够更改值。这比创建新节点和替换整个节点要简单得多,而且肯定更快,侵入性更小。
回答by Lol4t0
what is the problem. What sort of values do you want to write? For example, the fallowing code converts this xml
问题是什么。你想写什么样的价值观?例如,空闲代码转换此 xml
<?xml version="1.0" encoding="UTF-8"?>
<document>
<node attribute="value">
<inner_node inner="true"/>
text
</node>
</document>
to
到
<?xml version='1.0' encoding='UTF-8'?>
<document>
<new_amazing_tag_name attribute="foo">
<bar inner="true"/>new amazing text</new_amazing_tag_name>
</document>
Code:
代码:
QFile file (":/xml/document");
file.open(QIODevice::ReadOnly);
QDomDocument document;
document.setContent(&file);
QDomElement documentTag = document.documentElement();
qDebug()<<documentTag.tagName();
QDomElement nodeTag = documentTag.firstChildElement();
qDebug()<<nodeTag.tagName();
nodeTag.setTagName("new_amazing_tag_name");
nodeTag.setAttribute("attribute","foo");
nodeTag.childNodes().at(1).setNodeValue("new amazing text");
QDomElement innerNode = nodeTag.firstChildElement();
innerNode.setTagName("bar");
file.close();
QFile outFile("xmlout.xml");
outFile.open(QIODevice::WriteOnly);
QTextStream stream;
stream.setDevice(&outFile);
stream.setCodec("UTF-8");
document.save(stream,4);
outFile.close();
回答by docsteer
Here is a version of your code that does what you need. Note as spraff said, the key is finding the child of the "firstchild" node of type text - that's where the text lives in the DOM.
这是您的代码版本,可以满足您的需求。请注意,正如 spraff 所说,关键是找到文本类型的“firstchild”节点的子节点——这是文本在 DOM 中的位置。
QFile xmlFile(".\iWantToEdit.xml");
xmlFile.open(QIODevice::ReadWrite);
QByteArray xmlData(xmlFile.readAll());
QDomDocument doc;
doc.setContent(xmlData);
// Get the "Root" element
QDomElement docElem = doc.documentElement();
// Find elements with tag name "firstchild"
QDomNodeList nodes = docElem.elementsByTagName("firstchild");
// Iterate through all we found
for(int i=0; i<nodes.count(); i++)
{
QDomNode node = nodes.item(i);
// Check the node is a DOM element
if(node.nodeType() == QDomNode::ElementNode)
{
// Access the DOM element
QDomElement element = node.toElement();
// Iterate through it's children
for(QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())
{
// Find the child that is of DOM type text
QDomText t = n.toText();
if (!t.isNull())
{
// Print out the original text
qDebug() << "Old text was " << t.data();
// Set the new text
t.setData("Here is the new text");
}
}
}
}
// Save the modified data
QFile newFile("iEditedIt.xml");
newFile.open(QIODevice::WriteOnly);
newFile.write(doc.toByteArray());
newFile.close();