首页 > 开发 > 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("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() 函数编码后再输出,否这可能出现问题

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表