Get attributes and values within the XML body element in PHP

Description

The following code shows how to get attributes and values within the XML body element.

Example


//from   w  w  w.  ja  v a 2 s .c o  m
<?php
   $note=<<<XML
       <note>
       <to>Work</to>
       <from>Home</from>
        <heading>Reminder</heading>
       <body date="2013-01-01" type="private">This is a message.</body>
       </note>
XML;

   $xml=simplexml_load_string($note);
   foreach($xml->body[0]->attributes() as $a => $b){
      echo $a,'="',$b,"\"<br>";
   }
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser