当前位置: 首页 > 图文教程 > 网络编程 > PHP > 用PHP5的SimpleXML解析XML文档

PHP
PHP实例:用PHP简单实现多条件查询
PHP实例:用PHP实现多文件上载系统程序
PHP实例程序:用PHP制作登录页面程序
PHP实例:PHP取GB2312编码字符串首字母的方法
PHP实例:用PHP实现表单验证码登陆校验
Oracle与PHP实例开发Myers订单跟踪系统
PHP实例:email address 生成图片程序
PHP连接远程MYSQL和MYSQL5.1中文乱码处理方法
用 PHP 构建自定义搜索引擎
详细讲解PHP的Jmai组件及发送邮件实例
在动态网页技术PHP5中类(CLASS)的新特征
实例学习PHP如何实现在线发邮件
PHP上传文件的代码
不需要GD库的情况下实现验证码
PHP进阶教程:实现网站的无限分类
童虎:人人皆可做插件 Discuz! 插件开发实例讲解
创建论坛专业知识库 HDWiki(For Discuz!)V1.0正式版发布
PHP网站后门的隐藏技巧测试报告
加速PHP动态网站 MySQL索引分析和优化
php中rename()函数的妙用

用PHP5的SimpleXML解析XML文档


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 102 ::
收藏到网摘: n/a

 

以下为引用的内容:
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() 函数编码后再输出,否这可能出现问题