Hi,
Im using QDomDocument for xml file.
My xml file has the following data,
<?xml version="1.0"?>
<config>
<dbus>
<gas_value>2</gas_value>
</dbus>
</config>
<?xml version="1.0"?>
<config>
<dbus>
<gas_value>2</gas_value>
</dbus>
</config>
To copy to clipboard, switch view to plain text mode
I want to replace <gas_value>2</gas_value> with <gas_value>100</gas_value>
Im using following code to replace it
{
qDebug() << "unable to open xml file";
return;
}
doc.setContent(file);
QDomElement gasvalue
= parent.
firstChildElement("gas_value");
parent.firstChildElement("gas_value").firstChild().setNodeValue("100");
gasvalue.save(stream, 4);
file->close();
QFile *file = new QFile("dbus_write.xml");
if (!file->open(QIODevice::ReadWrite | QIODevice::Text))
{
qDebug() << "unable to open xml file";
return;
}
QDomDocument doc("dbus_write");
doc.setContent(file);
QDomElement root = doc.firstChildElement("config");
QDomElement parent = root.firstChildElement("dbus");
QDomElement gasvalue = parent.firstChildElement("gas_value");
parent.firstChildElement("gas_value").firstChild().setNodeValue("100");
QTextStream stream(file);
gasvalue.save(stream, 4);
file->close();
To copy to clipboard, switch view to plain text mode
but this code doesn't replaces the tag <gas_value>2</gas_value> , instead it creates another tag <gas_value>100</gas_value>
and produces following xml file
<?xml version="1.0"?>
<config>
<dbus>
<gas_value>2</gas_value>
</dbus>
</config>
<gas_value>100</gas_value>
<?xml version="1.0"?>
<config>
<dbus>
<gas_value>2</gas_value>
</dbus>
</config>
<gas_value>100</gas_value>
To copy to clipboard, switch view to plain text mode
I tried using replaceChild() method, but results in same output.
Any thoughts?
thanks.
Bookmarks