dimanche 17 octobre 2010

Use of attribute in simpleXml is counter intuitive

Given this file


<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<reports>
<report id="1"/>
</reports>
XML;
?>


Accessing the id is attribute is done this way


$xml = new SimpleXMLElement($xmlstr);
$id = (string) $xml->report["id"];


If you forget the cast (string) you got an illegal offset warning, wich is due to the fact that attributes themselves are simpleXMLElement.

Aucun commentaire: