PHP simplexml_load_string() 函數
實例
轉換形式良好的 XML 字符串為 SimpleXMLElement 對象,然后輸出對象的鍵和元素:
?php
$note=XML
note>
to>Tove/to>
from>Jani/from>
heading>Reminder/heading>
body>Don't forget me this weekend!/body>
/note>
XML;
$xml=simplexml_load_string($note);
print_r($xml);
?>
定義和用法
simplexml_load_string()
函數轉換形式良好的 XML 字符串為 SimpleXMLElement 對象。
語法
simplexml_load_string( _data,classname,options,ns,is_prefix_ );


實例 1
輸出 XML 字符串中每個元素的數據:
?php
$note=XML
note>
to>Tove/to>
from>Jani/from>
heading>Reminder/heading>
body>Don't forget me this weekend!/body>
/note>
XML;
$xml=simplexml_load_string($note);
echo $xml->to . "br>";
echo $xml->from . "br>";
echo $xml->heading . "br>";
echo $xml->body;
?>
實例 2
輸出 XML 字符串中每個子節點的元素名稱和數據:
?php
$note=XML
note>
to>Tove/to>
from>Jani/from>
heading>Reminder/heading>
body>Don't forget me this weekend!/body>
/note>
XML;
$xml=simplexml_load_string($note);
echo $xml->getName() . "br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "br>";
}
?>
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
您可能感興趣的文章:- PHP安裝memcache擴展的步驟講解
- PHP配置ZendOpcache插件加速
- PHP convert_uudecode()函數講解
- PHP安裝BCMath擴展的方法
- PHP convert_cyr_string()函數講解
- PHP chunk_split()函數講解
- PHP chr()函數講解
- PHP chop()函數講解
- PHP bin2hex()函數基礎實例講解
- PHP count_chars()函數講解