简体   繁体   中英

no value return for php xml simpleXMLElement

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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM