PHP, XML - Get child nodes and their attributes PHP, XML - Get child nodes and their attributes codeigniter codeigniter

PHP, XML - Get child nodes and their attributes


$XML = <<<XML<parent>   <seatmap id="1">      <seat row="A" seatnum="01" available="1" />      <seat row="A" seatnum="02" available="1" />      <seat row="A" seatnum="03" available="1" />      <seat row="A" seatnum="04" available="1" />      <seat row="A" seatnum="05" available="1" />    </seatmap></parent>XML;$xml_nodes = new SimpleXMLElement($XML);$nodes = $xml_nodes->xpath('//seatmap[@id = "1"]/seat'); // Replace the ID value with whatever seatmap id you're trying to accessforeach($nodes as $seat){    // You can then access: $seat['row'], $seat['seatnum'], $seat['available']}


Easily can be done with DOM:

$dom = new DOMDocument;$dom->load('xmlfile.xml');$xpath = new DOMXPath($dom);$seats = $xpath->query('//seatmap[@id="1"]/seat');if ($seats->length) {    foreach ($seats as $seat) {        echo "row: ".$seat->getAttribute('row').PHP_EOL;        echo "seatnum: ".$seat->getAttribute('seatnum').PHP_EOL;        echo "available: ".$seat->getAttribute('available').PHP_EOL;    }} else {    die('seatmap not found or is empty');}