remove node inside node in XML using PHP

I want to remove a specific node from my XML file and seems I missed something in code !can you help me figure it out , for example I want to delete tag and all of its inner tags .XML structure :\[code\] <students> <student> <username>sally</username> <subjects> <subjects>sub1</subjects> <subjects>sub2</subjects> <subjects>sub3</subjects> </subjects> </student><student> <username>jojo</username> <subjects> <subjects>sub1</subjects> <subjects>sub2</subjects> <subjects>sub3</subjects> <subjects>sub4</subjects> <subjects>sub5</subjects> </subjects> </student> </students>\[/code\]function :\[code\] public static function delete_subjects($UserName, $fileName) { //delete from xml $xml = new DOMDocument(); $xml->load($fileName); $users = $xml->getElementsByTagName('student'); foreach ($users as $user) { $username = $user->getElementsByTagName('username')->item(0)->nodeValue; if ($username == $UserName) { $p=$user->getElementsByTagName('subjects')->item(0); $xml->documentElement->removeNode($p); break; } } $xml->save($fileName); }\[/code\]call : \[code\]delete_subjects('jojo', $fileName);\[/code\]
 
Back
Top