This topic contains 4 replies, has 3 voices, and was last updated by  Jeff Drumm 3 years, 7 months ago.

How does node.remove() work?

  • hi. I’m trying to remove certain child nodes from a parent node but I’m not sure how node.remove function work. The index parameter is supposed to be a string? Can someone explain how the function works?

    Hello ffayyca,

    Possibly it would be better to send this question to support@interfaceware.com. The node.remove() is thoroughly documented in Wiki. But if you have specific issue with implementation, it rather be handled as support question.

    node.remove() [top]
    Usage: Node:remove(Index)
    Removes a child node.
    Returns: nothing.
    Required parameters:
    * Node: The node from which a child is to be removed node tree.
    * Index: The index or name of the child to remove string.

    Sample Code

     
    -- recommended usage:
    local isleaf = Node:remove(Index)
    -- which is shorthand for:
    local isleaf = node.remove(Node, Index)

    These examples might give you some ideas:

    XML = [[
    
             
    
               Blah
    
               Blech
    
             
    
            ]]
    
    
    
    local X = xml.parse(XML)
    
    X.RootNode.NodeLevel1.NodeLevel2:remove(1) -- removes "Blah"
    
    X.RootNode.NodeLevel1:remove("NodeLevel2") -- removes 1st ... even if non-empty
    
    X.RootNode.NodeLevel1:remove(1) -- also removes 1st ... even if non-empty, but the 2nd NodeLevel2 is now the 1st 🙂

    or

    XML = [[
    
             
    
               Blah
    
               Blech
    
             
    
            ]]
     
    local X = xml.parse(XML)
    
    X.RootNode.NodeLevel1:remove(2) -- removes 2nd ..., in this case Blech

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    Lev, I noticed the documentation for node:remove() says that the function returns nothing. I’m curious as to why this is done in the example:

    local isleaf = Node:remove(Index)

    What is the purpose of the assignment to isleaf if nothing is returned?

    Thanks!

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    Hello Jeff, there is an overlook with this particular example in Wiki. We will be updating it shortly. Thank you for pointing this out.

    Lev.

You must be logged in to reply to this topic.