Count the children of the car nodes in PHP

Description

The following code shows how to count the children of the car nodes.

Example


//  ww w. j  a v a  2  s . co  m
<?php
   $xml=<<<XML
     <cars>
       <car name="XML">
       <child/>
       <child/>
       <child/>
       <child/>
     </car>
     <car name="CSS">
       <child/>
        <child/>
     </car>
   </cars>
XML;

   $elem=new SimpleXMLElement($xml);
   foreach ($elem as $car){
       printf("%s has %d children.<br>", $car['name'], $car->count());
   }
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser