how to change specific node value in xml using Dom php -


hi want change value of cost $6.0 color of cupcake red. how can achieve that..here 2 sample cupcake though have many cupcake in xml file..so want first find cupcake color red , change corresponding cupcake price whatever like..

<cupcake>     <name> cookies , cream</name>     <flavours>         <ingredient>chocolate cake</ingredient>         <ingredient>salted caramel buttercream</ingredient>         <ingredient>buttercream</ingredient>     </flavours>     <colors>         <color>red</color>     </colors>     <energy>1900.6cal</energy>     <cost>$22.50</cost> </cupcake>  <cupcake>     <name> killer carrot</name>     <flavours>         <ingredient>carrot spice cake</ingredient>         <ingredient>cream cheese frosting</ingredient>         <ingredient>candied carrots</ingredient>         <ingredient>chocolate</ingredient>     </flavours>     <colors>         <color>aqua</color>     </colors>     <energy>1500.0kj</energy>     <cost>$15.80</cost> </cupcake> 

and php file is

<?php $xml = new domdocument();   $xml->load('cupcakes.xml');   if ($xml->schemavalidate('cupcakes.xsd')==false)      die ('<div class="error">validation failed</div>');  $xsl = new domdocument();  $xsl->load('cupcakes.xsl');  $proc = new xsltprocessor();  $proc->importstylesheet($xsl); // attach xsl rules   echo $proc->transformtoxml($xml); echo "<hr/>";  echo "<h2> first cupcake having color red has changed cost value $6.0";   $a = $xml->getelementsbytagname('color'); foreach ($a->nodevalue $a){ if ($a = "red") $a->getelementsbytagname('cost')->nodevalue="$6.00"; }  echo $proc->transformtoxml($xml);  ?> 

you xml missing document element. not valid xml file.

domnode::getelementsbytagname() returns node list, not single node. $nodevalue property of domnode, not domnodelist. checking color value, not it. cupcake can have several colors. if use xpath can have conditions that:

$document = new domdocument(); $document->load($xmlfile); $xpath = new domxpath($document);  foreach ($xpath->evaluate('//cupcake[colors/color = "red"]/cost') $cost) {   $cost->nodevalue = '';   $cost->appendchild($document->createtextnode('$6.00')); }  echo $document->savexml(); 

output:

<?xml version="1.0"?> <cupcakes> <cupcake>     <name> cookies , cream</name>     <flavours>         <ingredient>chocolate cake</ingredient>         <ingredient>salted caramel buttercream</ingredient>         <ingredient>buttercream</ingredient>     </flavours>     <colors>         <color>red</color>     </colors>     <energy>1900.6cal</energy>     <cost>$6.00</cost> </cupcake>  <cupcake>     <name> killer carrot</name>     <flavours>         <ingredient>carrot spice cake</ingredient>         <ingredient>cream cheese frosting</ingredient>         <ingredient>candied carrots</ingredient>         <ingredient>chocolate</ingredient>     </flavours>     <colors>         <color>aqua</color>     </colors>     <energy>1500.0kj</energy>     <cost>$15.80</cost> </cupcake> </cupcakes> 

xpath allows fetch nodes , scalar values. case:

fetch cupcake nodes...
//cupcake

... color node equals "red"...
//cupcake[colors/color = "red"]

...and cost child node:
//cupcake[colors/color = "red"]/cost


Comments