How to delete child node from xml using php -


$xpath = new domxpath($xml);  foreach($xpath->query("/root/info[name = '$c_name']") $node) {     $node->parentnode->removechild($node); } 

am getting error when executing code on localhost.

warning: domxpath::query(): invalid expression

i guess there error in foreach loop.

your code misses error handling , way create xpath expression prone injection can break it.

$xpath = new domxpath($xml);  $expression = "/root/info[name = '$c_name']";  $result = $xpath->query($expression);  if (false === $result) {     throw new invalidargumentexception(         sprintf(             'xpath expression %s failed input %s'             , var_export($expression, true)             , var_export($c_name, true)         )     ); }  foreach($result $node) {     $node->parentnode->removechild($node); } 

this example not yet handle injection issue introduces proper error handling realize possible error occurs.

if analyze further might spot problem single quotes. previous q&a covers topic:


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -