I need to edit the text of a QDomElement - Eg
I have an XML file with its content as -
<root>
<firstchild>Edit text here</firstchild>
</root>
How do I edit the text of the child element <firstchild>
?
I don't see any functions in the QDomElement of QDomDocument classes descriptions provided in Qt 4.7
Edit1 - I am adding more details.
I need to read, modify and save an xml file. To format of the file is as below -
<root>
<firstchild>Edit text here</firstchild>
</root>
The value of element needs to be edited.I code to read the xml file is -
QFile xmlFile(".\\iWantToEdit.xml"); xmlFile.open(QIODevice::ReadWrite); QByteArray xmlData(xmlFile.readAll()); QDomDocument doc; doc.setContent(xmlData);
// Read necessary values
// write back modified values?
Note: I have tried to cast a QDomElement to QDomNode and use the function setNodeValue(). It however is not applicable to QDomElement.
Any suggestions, code samples, links would we greatly welcome.
Go up an abstraction level to QDomNode.
firstchild
is a QDomText element so you can getvalue()
andsetValue(x)
to work with the text itself.what is the problem. What sort of values do you want to write? For example, the fallowing code converts this xml
to
Code:
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.
This will do what you want (the code you posted will stay as is):
... 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.
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:
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.