PHP simplexml_load_string()函數實例講解

今天小編就爲大家分享一篇關於PHP simplexml_load_string()函數實例講解,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

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>"; 
} 
?>

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對神馬文庫的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章