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>"; } ?>
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。