DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 用PHP5的SimpleXML解析XML文檔
用PHP5的SimpleXML解析XML文檔
編輯:XML詳解     
essages.XML
========================================================
<?XML version="1.0" ?>
<!--Sample XML document -->
<SystemMessage>
     <MessageTitle>System Down for Maintenance</MessageTitle>
    <MessageBody>Going down for maintenance soon!</MessageBody>
    <MessageAuthor>
   <MessageAuthorName>Joe SystemGod</MessageAuthorName>
   <MessageAuthorEmail>[email protected]</MessageAuthorEmail>
    </MessageAuthor>
    <MessageDate>March 4, 2004</MessageDate>
   <MessageNumber>10</MessageNumber>
</SystemMessage>

========================================================

XML 是一種創建元數據的語言,元數據是描述其它數據的數據,PHP中的XML處理是基於LIBXML2的,安裝時默認開啟。

可以通過PHPinfo()函數查看是否開啟了XML處理模塊,DOM,LIBXML,SAMPLEXML。

首先,通過samplexml_load_file函數把xml文件加載到一個對象中,sampleXML_load_file可以用戶遠程文件.
例如:

$xml = samplexml_load_file("messages.XML"); // 本地文件系統,當前目錄
$xml = sampleXML_load_file("http://www.xml.org.cn/messages.XML"); // 遠程web服務器

用 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() 函數編碼後再輸出,否這可能出現問題

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved