国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

用PHP5的SimpleXML解析XML文檔

2024-05-04 23:04:44
字體:
來源:轉載
供稿:網友
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 是一種創建元數據的語言,元數據是描述其它數據的數據,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("

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲阜市| 子洲县| 邵阳市| 株洲县| 西乌| 枣庄市| 清徐县| 北碚区| 新野县| 汾西县| 宜黄县| 德阳市| 朔州市| 民乐县| 邢台县| 岚皋县| 红安县| 新安县| 邢台县| 齐河县| 浦东新区| 黄浦区| 高陵县| 东平县| 新竹市| 永修县| 东源县| 福贡县| 通州区| 正蓝旗| 岢岚县| 班玛县| 尼玛县| 金溪县| 永春县| 门头沟区| 阳春市| 达州市| 图们市| 新余市| 新余市|