返回的是一個迭代對象,不是一個數組
定義和用法
simplexml_load_string() 函數把 XML 字符串載入對象中。
如果失敗,則返回 false。
語法
simplexml_load_file(string,class,options,ns,is_prefix)
| 參數 | 描述 |
|---|---|
| string | 必需。規定要使用的 XML 字符串。 |
| class | 可選。規定新對象的 class。 |
| options | 可選。規定附加的 Libxml 參數。 |
| ns | 可選。 |
| is_prefix | 可選。 |
返回值
返回類 SimpleXMLElement 的一個對象,該對象的屬性包含 XML 文檔中的數據。如果失敗,則返回 false。
例子
<?php
$xmlstring = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
XML;
$xml = simplexml_load_string($xmlstring);
var_dump($xml);
?>
輸出:
object(SimpleXMLElement)#1 (4)
{
["to"]=> string(4) "George"
["from"]=> string(4) "John"
["heading"]=> string(8) "Reminder"
["body"]=> string(29) "Don't forget the meeting!"
}
