Hello and welcome to our community! Is this your first visit?
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
    Thanked 2,224 Times in 2,211 Posts

    DomDocument->change parent of a node.

    Hi all,
    I've an xml file with the following DTD
    <!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){
    $dom = new DomDocument('1.0''UTF-8');
    $dom->validateOnParse true;
    $childnodes $node->getElementsByTagName('group');
    // making sure that the $newGroupId is not a child group     
    foreach($childnodes as $child){
    false//not possible to move 
    $newnode $dom->createElement('group');
    # Creating a new node by getting current values
    $value1 $dom->createTextNode($node->getAttribute('xml:id'));
    $value2 $dom->createTextNode($node->getAttribute('name'));
    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

    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);    

    Any help would be apreciated.

    Last edited by abduraooft; 08-23-2009 at 12:59 PM.
    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