php - Finding the value of a child in a specific attribute -


<data>     <gig id="1">     <date>december 19th</date>     <venue>the zanzibar</venue>     <area>liverpool</area>     <telephone>ticketline.co.uk</telephone>     <price>£6</price>     <time>time tba</time> </gig> <gig id="2">     <date>sat. 16th jan</date>     <venue>celtic connection, classic grand</venue>     <area>glasgow</area>     <telephone>0141 353 8000</telephone>     <price>£17.50</price>     <time>7pm</time> </gig> 

say if wanted view values of "date" gig element has attribute of 2 how using php ?

basically want delete id 2 , create again or modify it.

using simplexml how can delete part ?

to find nodes, use xpath.

$data->xpath('//gig[@id="2"]'); 

it return array <gig/> nodes attribute id value 2. usually, contain 0 or 1 element. can modify directly. example:

$data = simplexml_load_string(     '<data>         <gig id="1">             <date>december 19th</date>             <venue>the zanzibar</venue>             <area>liverpool</area>             <telephone>ticketline.co.uk</telephone>             <price>£6</price>             <time>time tba</time>         </gig>         <gig id="2">             <date>sat. 16th jan</date>             <venue>celtic connection, classic grand</venue>             <area>glasgow</area>             <telephone>0141 353 8000</telephone>             <price>£17.50</price>             <time>7pm</time>         </gig>     </data>' );  $nodes = $data->xpath('//gig[@id="2"]');  if (empty($nodes)) {     // didn't find }  $gig = $nodes[0]; $gig->time = '6pm';  die($data->asxml()); 

deleting arbitrary nodes order of magnitude more complicated, it's easier modify values rather deleting/recreating node.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -