PDA

View Full Version : remove node in xml file



mattia
6th March 2008, 13:16
Hello, i need to create a function to remove a whole xml Node, in my case i need to remove the node starting with "group" tag.
This is a my xml file snip:


<?xml version='1.0' encoding='UTF-8'?>
<twm version="1.0" modify_date="2008-03-06T13:03:19" creation_date="2008-03-06T13:03:19">
<group name="test" > // remove
<option key="key" tagName="tagValue" /> // remove
</group> // remove
</twm>

and this is the code used to remove the node, but it doesn not work.


bool twmXMLObject::removeGroup ( QDomDocument * twmDomDocument , QString theGroupName )
{
QDomNodeList domTwm = twmDomDocument->elementsByTagName( "group" );

if ( !domTwm.isEmpty() )
{
for ( uint i = 0; i < domTwm.length(); i++ )
{
if ( domTwm.at(i).toElement().attribute( "name" ) == theGroupName )
{
twmDomDocument->removeChild( domTwm.at(i) );
}
}
}
}

After call the function removeGroup i save the xml file.
Any idea?
thx

jpn
6th March 2008, 13:25
From QDomNode::removeChild() docs:

Removes oldChild from the list of children. oldChild must be a direct child of this node.
In your case it's not a direct child of the document node. So use QDomNode::parentNode() or QDomDocument::documentElement() and remove it from the direct parent.