用 var_dump($xml) 和 print_r($xml) 分別輸出其結構.var_dump給出了變量的類型和長度,而print_r可讀性更強
輸出對象中的所有元素名稱和它的值.
echo $xml->messagetitle; //輸出消息的標題
echo $xml->messagebody; // 輸出消息體
echo $xml->messageauthor; //消息的作者
echo $xml->messagedate; // 消息產生的日期
echo $xml->messagenumber; // 消息代碼
===================================================
另外,還有一個函數,可以把xml字符串加載到一個simplexml對象中取
$channel =<<<_xml_
<channel>
<title>what's for dinner</title>
<link>http://menu.example.com/</link>
<description>these are your choices of what to eat tonight. </description>
</channel>
_xml_;
$xml = simplexml_load_string($channel);
===================================================
rss.xml
=============================================
<?xml version="1.0" encoding="utf-8"?>
<rss version="0.91">
<channel>
<title>what's for dinner</title>
<link>http://menu.example.com/</link>
<description>these are your choices of what to eat tonight.</description>
<item>
<title>braised sea cucumber</title>
<link>http://menu.example.com/dishes.php?dish=cuke</link>
<description>gentle flavors of the sea that nourish and refresh you. </description>
</item>
<item>
<title>baked giblets with salt</title>
<link>http://menu.example.com/dishes.php?dish=giblets</link>
<description>rich giblet flavor infused with salt and spice. </description>
</item>
<item>
<title>abalone with marrow and duck feet</title>
<link>http://menu.example.com/dishes.php?dish=abalone</link>
<description>there's no mistaking the special pleasure of abalone. </description>
</item>
</channel>
</rss>
=====================================================
1.訪問具有相同元素名稱的節點
2.通過foreach循環所有相同元素名稱的子節點
foreach($xml->channel->item as $key=>$value)
{
print "title: " . $item->title . "/n";
}
3.輸出整個文檔
echo $xml->asxml();
4.把節點作為字符串輸出
echo $xml->channel->item[0]->asxml();
這將輸出文本
<item>
<title>braised sea cucumber</title>
<link>http://menu.example.com/dishes.php?dish=cuke</link>
<description>gentle flavors of the sea that nourish and refresh you. </description>
</item>
帶文件名參數的asxml將會把原本輸出的內容保存為一個文件
$xml->channel->item[0]->asxml("item[0].xml");
完整的代碼:
rss.xml
=====
<?xml version="1.0" encoding="utf-8"?>
<rss version="0.91">
<channel>
<title>what's for dinner</title>
<link>http://menu.example.com/</link>
<description>these are your choices of what to eat tonight.</description>
<item>
<title>braised sea cucumber</title>
<link>http://menu.example.com/dishes.php?dish=cuke</link>
<description>gentle flavors of the sea that nourish and refresh you. </description>
</item>
<item>
<title>baked giblets with salt</title>
<link>http://menu.example.com/dishes.php?dish=giblets</link>
<description>rich giblet flavor infused with salt and spice. </description>
</item>
<item>
<title>abalone with marrow and duck feet</title>
<link>http://menu.example.com/dishes.php?dish=abalone</link>
<description>there's no mistaking the special pleasure of abalone. </description>
</item>
</channel>
</rss>
rss.php
======
<?php
$xml = simplexml_load_file("rss.xml");
echo "<h3>".$xml->channel->title."</h3><br>";
echo "<ul>";
echo "<li>title:".$xml->channel->item[0]->title."</li>";
echo "<li>title:".$xml->channel->item[1]->title."</li>";
echo "<li>title:".$xml->channel->item[2]->title."</li>";
echo "</ul>";
print "title: " . $xml->channel->item[0]->title . "/n<br>";
print "title: " . $xml->channel->item[1]->title . "/n<br>";
print "title: " . $xml->channel->item[2]->title . "/n<br>";
echo "<hr>";
foreach ($xml->channel->item[0] as $element_name => $content) {
print "the $element_name is $content/n<br>";
}
echo "<hr>";
print_r($xml);
echo $xml->channel->item[0]->asxml();
?>
任何xml文本在輸出前最好用 htmlentiteis() 函數編碼后再輸出,否這可能出現問題