How to set a node value using QDOM ?
-
wrote on 2 Mar 2018, 13:12 last edited by kishore_hemmady 3 Feb 2018, 13:13
My XML looks something like this
<main>
<child1>
<Content>Video</Content>
<ZoneNumber>4</ZoneNumber>
<Height>400</Height>
<Width>600</Width>
.
.
.
</child1>
.
.
.
</main>I want to edit "height" tag's value from 400 to say 500 using DOM. I used node.setvalue("500") function for height tag its not working.Please suggest.
-
wrote on 2 Mar 2018, 19:14 last edited by
Hi @kishore_hemmady,
I want to edit "height" tag's value from 400 to say 500 using DOM
You can do it a few ways, including:
dom.elementsByTagName("Height").at(0).firstChild().setNodeValue("500");
One thing that often catches people (well, caught me often in the past) is that the
Height
node does not contain the400
text within the node itself, but rather the node has a child text node that contains the text, hence thefirstChild()
in the example above.To show it working:
qDebug() << "== before =="; qDebug().noquote() << dom.toString(); qDebug() << dom.elementsByTagName("Height").at(0).firstChild().nodeValue(); dom.elementsByTagName("Height").at(0).firstChild().setNodeValue("500"); qDebug() << "== after =="; qDebug() << dom.elementsByTagName("Height").at(0).firstChild().nodeValue(); qDebug().noquote() << dom.toString();
Output:
== before == <main> <child1> <Content>Video</Content> <ZoneNumber>4</ZoneNumber> <Height>400</Height> <Width>600</Width> </child1> </main> "400" == after == "500" <main> <child1> <Content>Video</Content> <ZoneNumber>4</ZoneNumber> <Height>500</Height> <Width>600</Width> </child1> </main>
Cheers.
-
wrote on 5 Mar 2018, 04:52 last edited by
@Paul-Colby Hi,Thanks for the reply.
the value is getting changed but its not getting written into the XML.I am able to print the height tag's first child node value before and after setting.But in my XML still the old value is present. -
@Paul-Colby Hi,Thanks for the reply.
the value is getting changed but its not getting written into the XML.I am able to print the height tag's first child node value before and after setting.But in my XML still the old value is present.wrote on 5 Mar 2018, 07:20 last edited byHi @kishore_hemmady,
its not getting written into the XML ... in my XML still the old value is present.
I assume you mean its not getting written into the XML file. XML itself is a language, and DOM is an object model representing the data described by the language (in this case, parsed into RAM). If you the the changes to the DOM reflected back to an XML file somewhere, you need to write it out yourself. Its not hard to do though, for example:
QFile file("file.xml"); // Load the XML content from file into a DOM document. file.open(QFile::ReadOnly); QDomDocument dom; dom.setContent(&file); file.close(); // Change the DOM in memory. dom.elementsByTagName("Height").at(0).firstChild().setNodeValue("500"); // Write the DOM as XML content back to the file. file.open(QFile::WriteOnly); file.write(dom.toByteArray(1)); file.close();
Of course, you should do error checking, and probably backup the file before re-writing it.
For the example above, a
diff
of thefile.xml
file before and after looks like:--- file.xml 2018-03-05 18:07:36.638367812 +1100 +++ file.xml 2018-03-05 18:11:32.357453075 +1100 @@ -2,7 +2,7 @@ <child1> <Content>Video</Content> <ZoneNumber>4</ZoneNumber> - <Height>400</Height> + <Height>500</Height> <Width>600</Width> </child1> </main>
Cheers.
1/4