alizadeh91
2nd October 2012, 17:06
Hi,
I wanna to open a xml file and update or add some elements and node into it. I've approximately done this but the problem is that it's not updating and add updated stream in end of xml file.
I think i must clear previous stream or file and then save it but i don't know how..
Here is my code:
In the meanwhile how can i clear a text file??!!!
QDomDocument doc("name");
QFile file(filePath);
if(!file.open( QFile::WriteOnly | QFile::ReadOnly | QFile::Text))
return false;
if(!doc.setContent(&file));
{
file.close();
return false;
}
//Parse XML Document
QDomElement docElem = doc.documentElement();
//finding an element
QDomElement dialsElement;
dialsElement = docElem.elementsByTagName("GaugeComponents").at(0).toElement().elementsByTagName("Dials").at(0).toElement();
//creating some elements
QDomElement dialElement = doc.createElement("Dial2");
QDomElement x_dialElement = doc.createElement("x");
x_dialElement.setNodeValue("hello");
dialElement.appendChild(x_dialElement);
dialsElement.appendChild(dialElement);
QTextStream out(&file);
doc.save(out,0);
file.close();
I wanna to open a xml file and update or add some elements and node into it. I've approximately done this but the problem is that it's not updating and add updated stream in end of xml file.
I think i must clear previous stream or file and then save it but i don't know how..
Here is my code:
In the meanwhile how can i clear a text file??!!!
QDomDocument doc("name");
QFile file(filePath);
if(!file.open( QFile::WriteOnly | QFile::ReadOnly | QFile::Text))
return false;
if(!doc.setContent(&file));
{
file.close();
return false;
}
//Parse XML Document
QDomElement docElem = doc.documentElement();
//finding an element
QDomElement dialsElement;
dialsElement = docElem.elementsByTagName("GaugeComponents").at(0).toElement().elementsByTagName("Dials").at(0).toElement();
//creating some elements
QDomElement dialElement = doc.createElement("Dial2");
QDomElement x_dialElement = doc.createElement("x");
x_dialElement.setNodeValue("hello");
dialElement.appendChild(x_dialElement);
dialsElement.appendChild(dialElement);
QTextStream out(&file);
doc.save(out,0);
file.close();