Results 1 to 2 of 2

Thread: remove node in xml file

  1. #1
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default remove node in xml file

    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:
    Qt Code:
    1. <?xml version='1.0' encoding='UTF-8'?>
    2. <twm version="1.0" modify_date="2008-03-06T13:03:19" creation_date="2008-03-06T13:03:19">
    3. <group name="test" > // remove
    4. <option key="key" tagName="tagValue" /> // remove
    5. </group> // remove
    6. </twm>
    To copy to clipboard, switch view to plain text mode 
    and this is the code used to remove the node, but it doesn not work.
    Qt Code:
    1. bool twmXMLObject::removeGroup ( QDomDocument * twmDomDocument , QString theGroupName )
    2. {
    3. QDomNodeList domTwm = twmDomDocument->elementsByTagName( "group" );
    4.  
    5. if ( !domTwm.isEmpty() )
    6. {
    7. for ( uint i = 0; i < domTwm.length(); i++ )
    8. {
    9. if ( domTwm.at(i).toElement().attribute( "name" ) == theGroupName )
    10. {
    11. twmDomDocument->removeChild( domTwm.at(i) );
    12. }
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    After call the function removeGroup i save the xml file.
    Any idea?
    thx

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: remove node in xml file

    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.
    J-P Nurmi

  3. The following 2 users say thank you to jpn for this useful post:

    mattia (6th March 2008), metdos (16th July 2010)

Similar Threads

  1. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  2. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10
  5. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 08:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.