سؤال

So far i have not been able to return any value for id. here is my code.

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>

<Benchmark id="IE8"></Benchmark>

XML;

$sxml = new SimpleXMLElement($xml);

var_dump($sxml);

$name = (string) $sxml->Benchmark['id'];

echo $name;

this is my var dump

object(SimpleXMLElement)[1]
  public '@attributes' => 
    array (size=1)
      'id' => string 'IE8' (length=3)

I basically just want to return the id value: IE8 in Benchmark

هل كانت مفيدة؟

المحلول

When you create a new SimpleXMLElement, the returned object represents the root element of your XML file.

$sxml is your <Benchmark> element, $sxml->Benchmark doesn't exist.

$name = (string) $sxml['id'];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top