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,
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){
$xmlId= explode('_', $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);

$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

Get all the chidnodes of 'this' node and make sure that the parent is not going to be a child of 'this'
Create a new node by getting the attributes of 'this' node and after that delete 'this' node from DOM
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
$newnode = $node->parentNode->removeChild($node);
$dom->getElementById('id_'.$newGroupId)->appendChild($newnode); :o