PHP simplexml_load_string()函數(shù)實例講解
PHP simplexml_load_string() 函數(shù)
實例
轉(zhuǎn)換形式良好的 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()函數(shù)轉(zhuǎn)換形式良好的 XML 字符串為 SimpleXMLElement 對象。
語法
simplexml_load_string( _data,classname,options,ns,is_prefix_ );


實例 1
輸出 XML 字符串中每個元素的數(shù)據(jù):
<?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 字符串中每個子節(jié)點的元素名稱和數(shù)據(jù):
<?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>";
}
?>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
php for 循環(huán)語句使用方法詳細(xì)說明
for 循環(huán)是 PHP 中最復(fù)雜的循環(huán)結(jié)構(gòu)。它的行為和 C 語言的相似。在PHP中使用的是執(zhí)行相同的代碼集的次數(shù)2010-05-05
PHP chmod 函數(shù)與批量修改文件目錄權(quán)限
chmod() 函數(shù)改變文件模式。chmod — Changes file mode 如果成功則返回 TRUE,否則返回 FALSE。2010-05-05

