Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1

    DomDocument->change parent of a node.

    Hi all,
    I've an xml file with the following DTD
    Code:
    <!ELEMENT discussionGroups ( group+ ) >
    <!ATTLIST discussionGroups xml:id NMTOKEN #REQUIRED >
    
    <!ELEMENT group ( group* ) >
    <!ATTLIST group name CDATA #REQUIRED >
    <!ATTLIST group xml:id ID #REQUIRED >
    I need to move a group-node from the current position and attach as a child of another element. I'm just learning xml, so I couldn't make the expected output.
    Here is my current code,
    PHP Code:
    public function transferGroup($newGroupId){
            
    $xmlPath=_ROOT_._INCLUDE_DIR_.'/groups.xml';
            
    $dom = new DomDocument('1.0''UTF-8');
            
    $dom->validateOnParse true;
            
    $dom->load($xmlPath);
            
    $node=$dom->getElementById('id_'.$this->GroupId);
            
    $childnodes $node->getElementsByTagName('group');
            
    // making sure that the $newGroupId is not a child group     
            
    foreach($childnodes as $child){
                
    $xmlIdexplode('_'$child->getAttribute('xml:id'));
                if(
    $xmlId[1]==$newGroupId)
                    return 
    false//not possible to move 
             
    }
            
    $this->objDB->Update('group',array('parent_id'=>$newGroupId),
                array(
    'group_id'=>$this->GroupId));
            
    $newnode $dom->createElement('group');
            
    # Creating a new node by getting current values
            
    $attr1=$dom->createAttribute('xml:id');
            
    $value1 $dom->createTextNode($node->getAttribute('xml:id'));
            
    $attr1->appendChild($value1); 
            
    $newnode->appendChild($attr1);
            
            
    $attr2=$dom->createAttribute('name');
            
    $value2 $dom->createTextNode($node->getAttribute('name'));
            
    $attr2->appendChild($value2); 
            
    $newnode->appendChild($attr2);
            
            
    $dom->removeChild($node);
            
    $dom->getElementById('id_'.$newGroupId)->appendChild($newnode);
            
    $dom->save($xmlPath);
            return 
    true;
        } 
    My intention is to change the parent of node having an id = $this->GroupId to the node having id=$newGroupId

    My current logic is
    1. Get all the chidnodes of 'this' node and make sure that the parent is not going to be a child of 'this'
    2. Create a new node by getting the attributes of 'this' node and after that delete 'this' node from DOM
    3. Append the newly created node as the child of node having id=$newGroupId


    Edit:
    I think it's working now after some struggle, but Is there a way to simplify the entire function?
    ---------------
    Got it. It's as simple as
    PHP Code:
    $newnode $node->parentNode->removeChild($node);    
        
    $dom->getElementById('id_'.$newGroupId)->appendChild($newnode); 


    Any help would be apreciated.

    Thanks.
    Last edited by abduraooft; 08-23-2009 at 11:59 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •