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
Post a Comment