PDA

View Full Version : Xml Qt4



hgedek
6th September 2007, 03:00
Is there a way of updating the text of an domelement?Or as I doing We have to create xml file again for only a text updating?

rajesh
6th September 2007, 06:34
I also got the same problem.
I didn't find any way to update a single entry in xml file.
now, I am reading whole xml file in some variables , updating and overwriting whole xml file.

I am looking for, is there any direct way to update a single entry in XML file?

hgedek, I think you have the same question?

hgedek
6th September 2007, 07:13
Yes thanks.We have the same problem.

marcel
6th September 2007, 07:28
What about QDomDocument::replaceChild?

Regards

hgedek
6th September 2007, 08:53
I tried replace child.It changed child info but created a new copy of xml file in same file too.


QFile programFile(PARAMETER_FILE);
programFile.open(QFile::ReadWrite);
QDomElement element;
XML_File xmlData;
bool result=false;

xmlData.read(&programFile);

element=xmlData.domDocument.documentElement();
if(element.tagName()=="Parameters" && element.hasChildNodes()==true)
{
element=element.firstChildElement();
do
{
if(element.tagName().toStdString()==parameter->getName())
{
xmlData.ChangeParameter(parameter,element,Indexes) ;
result=true;
break;
}
else
{
element=element.nextSiblingElement();
}
}while(!element.isNull());
}
__________________________________________________ ________________________
void XML_File::ChangeParameter(Parameter *parameter, QDomElement element,std::vector<unsigned int> Indexes)
{

QDomNodeList parameterList,elementList;
parameterList=element.childNodes();
unsigned int index;
QString tagName;
QDomElement oldTitleElement,newTitleElement,_element;
QDomText newTitleText;
vector<unsigned int>::iterator itr;

for(itr=Indexes.begin();itr!=Indexes.end();itr++)
{
_element=parameterList.item(*itr).toElement();
elementList=_element.childNodes();
for(index=0;index<(unsigned int)elementList.size();index++)
{
oldTitleElement=elementList.item(index).toElement( );
tagName=oldTitleElement.tagName();
if(tagName=="Value")
{
newTitleElement = domDocument.createElement("Value");
newTitleText = domDocument.createTextNode
(QString::number(parameter->Parameters[Indexes[*itr]]->getValue()));
newTitleElement.appendChild(newTitleText);
_element.replaceChild(newTitleElement, oldTitleElement);
}
}
}
}


This is my write update function.Change parameter do update.

hgedek
7th September 2007, 08:28
I understood that:
If we want to change anything on XML file we have use QTextStream object.For updating an text info we have use stream again.The functions of domDocument are doing their jobs but they dont update file dynamic.We have to write the last situation using QTextStream(using save( ) ).But this time everything in XML file are copied again in it under before ones.