日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

用PHP5的SimpleXML解析XML文檔_PHP教程

編輯Tag賺U幣
教程Tag:暫無(wú)Tag,歡迎添加,賺取U幣!

推薦:使用PHP生成1000個(gè)隨機(jī)注冊(cè)碼
一般程序中都需要用到注冊(cè)碼,為了防止盜版,如果把生成的注冊(cè)碼保存到數(shù)據(jù)庫(kù)里,并且通過(guò)軟件在客戶端訪問(wèn)服務(wù)器來(lái)匹配客戶端輸入的驗(yàn)證碼是否正確,這是一種好的解決盜版的方案。 下面描述

以下為引用的內(nèi)容:
messages.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 是一種創(chuàng)建元數(shù)據(jù)的語(yǔ)言,元數(shù)據(jù)是描述其它數(shù)據(jù)的數(shù)據(jù),PHP中的XML處理是基于LIBXML2的,安裝時(shí)默認(rèn)開(kāi)啟。

可以通過(guò)phpinfo()函數(shù)查看是否開(kāi)啟了XML處理模塊,DOM,LIBXML,SAMPLEXML。

首先,通過(guò)samplexml_load_file函數(shù)把xml文件加載到一個(gè)對(duì)象中,samplexml_load_file可以用戶遠(yuǎn)程文件。

例如:

$xml = samplexml_load_file("messages.xml"); // 本地文件系統(tǒng),當(dāng)前目錄

$xml = samplexml_load_file("http://www.xml.org.cn/messages.xml"); // 遠(yuǎn)程web服務(wù)器

用 var_dump($xml) 和 print_r($xml) 分別輸出其結(jié)構(gòu).var_dump給出了變量的類型和長(zhǎng)度,而print_r可讀性更強(qiáng)輸出對(duì)象中的所有元素名稱和它的值。

echo $xml->MessageTitle; //輸出消息的標(biāo)題

echo $xml->MessageBody; // 輸出消息體

echo $xml->MessageAuthor; //消息的作者

echo $xml->MessageDate;  // 消息產(chǎn)生的日期

echo $xml->MessageNumber;  // 消息代碼

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

另外,還有一個(gè)函數(shù),可以把XML字符串加載到一個(gè)simplexml對(duì)象中取。

以下為引用的內(nèi)容:

$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、訪問(wèn)具有相同元素名稱的節(jié)點(diǎn)

2、通過(guò)foreach循環(huán)所有相同元素名稱的子節(jié)點(diǎn)

以下為引用的內(nèi)容:
foreach($xml->channel->item as $key=>$value)
{
print "Title: " . $item->title . "\n";
}

3、輸出整個(gè)文檔

echo $xml->asXML();

4、把節(jié)點(diǎn)作為字符串輸出

echo $xml->channel->item[0]->asXML();

這將輸出文本

以下為引用的內(nèi)容:
<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>

帶文件名參數(shù)的asXML將會(huì)把原本輸出的內(nèi)容保存為一個(gè)文件

$xml->channel->item[0]->asXML("item[0].xml");

完整的代碼:

以下為引用的內(nèi)容:

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() 函數(shù)編碼后再輸出,否這可能出現(xiàn)問(wèn)題

分享:新手通過(guò)實(shí)例學(xué)習(xí)動(dòng)態(tài)網(wǎng)頁(yè)P(yáng)HP的語(yǔ)法
以下為引用的內(nèi)容: <?php echo "Hello, World!"; ?> 運(yùn)行結(jié)果: Hello, World! 變量標(biāo)記為“

來(lái)源:模板無(wú)憂//所屬分類:PHP教程/更新時(shí)間:2008-08-22
相關(guān)PHP教程