-1
$xpath = new DOMXPATH($xml);

foreach($xpath->query("/root/info[name = '$c_name']") as $node)
{
    $node->parentNode->removeChild($node);
}

Am getting this error when executing this code on localhost.

Warning: DOMXPath::query(): Invalid expression

I guess there is an error in the foreach loop.

hakre
  • 193,403
  • 52
  • 435
  • 836
Jay
  • 110
  • 5

1 Answers1

0

Your code misses error handling and the way you create the xpath expression is prone to injection which can also easily 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 for input %s'
            , var_export($expression, true)
            , var_export($c_name, true)
        )
    );
}

foreach($result as $node)
{
    $node->parentNode->removeChild($node);
}

This example does not yet handle the injection issue just introduces proper error handling so that you realize as early as possible where the error actually occurs.

If you then analyze it further you might spot a problem with single quotes. A previous Q&A covers that topic:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I have given more details about my problem,please follow this link: http://stackoverflow.com/questions/29878713/remove-nodes-from-xml-document-by-node-value – Jay Apr 26 '15 at 16:51
  • edit your question here, do not repost it. apart from that, the edit is an improvement so consider to move it here. also add how you check that editing the XML did (not) work. – hakre Apr 26 '15 at 20:19