PDA

View Full Version : how to modify the text of node in xml docment?



GChen
25th June 2008, 02:53
hi,everybody.

some xml docment like this:
<root><firstchild>text<firstchild></root>

i want to change the "text" to "upatetext",and i try the function "setNodeValue()",
but it doesn't work.

can anyone help me?
thanks!

fanat9
25th June 2008, 14:05
QDomDocument xmlDocument;
if ( !xmlDocument.setContent(&xmlFile, true, &errorStr, &errorLine, &errorColumn) )
{
....
}

QDomElement rootElement = xmlDocument.documentElement();
rootElement.firstChildElement("firstchild").firstChild().setNodeValue("upatetext");

aamer4yu
25th June 2008, 14:31
Just a comment -
Actually the text is a child node. You can identify it by node.isText() function.

Hence you need to get the firstchildelement of "firstchild" node :)

foggy-mind
26th February 2009, 09:23
hi,
I have one problem.

some xml
<?xml version='1.0'?>
<configs>
<dbName>database</dbName>
<userName>user</userName>
</configs>

when xml document is like this everithing work fine:

QDomElement rootElement = cfgData.documentElement();
rootElement.firstChildElement("dbName").firstChild().setNodeValue("newdbname");
rootElement.firstChildElement("userName").firstChild().setNodeValue("newusername");

file = new QFile("config.xml");
file->open(QIODevice::WriteOnly | QIODevice::Text);
xml = new QTextStream(file);
xml->operator <<(cfgData.toString());
file->close();
delete file;
delete xml;

when user name or db name set empty (this is need when I dont want to know the last connection data) and save the xml data into file, the tags
<dbName>database</dbName>
<userName>user</userName>
changed to <userName/> and <dbName/>. but when I wanna save new text into this tags with setNodeValue(). after saving xml data tags still empty. when I change the tags declaration like this <dbName>...</dbName> <userName>...</userName> eveithing work fine.

WHERE IS MY MISTAKE??? :)

thanks....

Lykurg
26th February 2009, 11:25
<dbName>database</dbName>
Here you have a DOM node.

<dbName />
Here you haven't so you must create one in qt and append it. Like

QDomText t = doc.createTextNode("Hello World");
dbName.appendChild(t);

foggy-mind
26th February 2009, 11:48
yea, your right. :) thanks.