PDA

View Full Version : qdom xml update



RenanBS
12th October 2010, 22:53
I'm trying to update a simple xml file without much success:


<?xml version='1.0' encoding='UTF-8'?>
<system_desktop_icons>
<trash>
<pos>@Point(0, 50)</pos>
<position>
<x>0</x>
<y>50</y>
</position>
</trash>
</system_desktop_icons>



This is my code:


QFile inputFile ("/home/renan/xmlparser/desktopIconPosition.xml");
if (!inputFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
qDebug() << "Failed to open file for reading.";
return false;
}

QDomDocument document;
if (!document.setContent (&inputFile))
{
qDebug() << "Failed to parse the file into a DOM tree.";
inputFile.close();
return false;
}

inputFile.close();

QDomElement documentElement = document.documentElement();
QDomNodeList elements = documentElement.elementsByTagName ("trash");

if (elements.size() == 1)
{
QDomNode node = documentElement.firstChild();
while( !node.isNull() )
{
if( node.isElement() )
{
QDomElement element = node.toElement();
qDebug() << "ELEMENT" << element.tagName();
qDebug() << "ELEMENT ATTRIBUTE NAME" << element.attribute( "name", "not set" );

}

if( node.isText() )
{
QDomText text = node.toText();
qDebug() << text.data();
}

node = node.nextSibling();
}
}

QFile outputFile ("/home/renan/xmlparser/desktopIconPosition.xml");
if (!outputFile.open (QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Failed to open file for writing.";
return false;
}

QTextStream stream (&outputFile);
stream << document.toString();

outputFile.close();


I want to change x and y values, but with this code I just get a qDebug output of trash. I never get the pos or position output.

I hope I was clear enough.

Any help?

Renan