电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 软件教学
用PHP5的SimpleXML解析XML文档
作者:未知 点击:0
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
以下为引用的内容:
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>systemgod@someserver.com
</MessageAuthorEmail>
    </MessageAuthor>
    <MessageDate>March 4, 2004</MessageDate> 中国站长.站
   <MessageNumber>10</MessageNumber>
</SystemMessage>
========================================================
中国站长_站,为中文网站提供动力

xml 是一种创建元数据的语言,元数据是描述其它数据的数据,PHP中的XML处理是基于LIBXML2的,安装时默认开启。

中.国.站长站

可以通过phpinfo()函数查看是否开启了XML处理模块,DOM,LIBXML,SAMPLEXML。 Chinaz@com

首先,通过samplexml_load_file函数把xml文件加载到一个对象中,samplexml_load_file可以用户远程文件。 [中国站长站]

例如: [中国站长站]

$xml = samplexml_load_file("messages.xml"); // 本地文件系统,当前目录

Chinaz_com


$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; //输出消息的标题 Chinaz

echo $xml->MessageBody; // 输出消息体

站.长站


echo $xml->MessageAuthor; //消息的作者 Www^Chinaz^com

echo $xml->MessageDate;  // 消息产生的日期 Www@Chinaz@com

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

中国站长_站,为中文网站提供动力


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

中.国站长站

另外,还有一个函数,可以把XML字符串加载到一个simplexml对象中取。

Www@Chinaz@com


以下为引用的内容:

$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>

Www_Chinaz_com

  <link>http://menu.example.com/dishes.php?dish=abalone</link>
  <description>There's no mistaking the special pleasure of abalone. </description>
 </item>
</channel>
</rss>
===================================================== Www~Chinaz~com



Chinaz_com


1、访问具有相同元素名称的节点

Chinaz.com

2、通过foreach循环所有相同元素名称的子节点 中国.站长站

以下为引用的内容:
foreach($xml->channel->item as $key=>$value)
{
print "Title: " . $item->title . "\n";
}
Chinaz^com

3、输出整个文档 中国站长_站,为中文网站提供动力

echo $xml->asXML(); 站.长站

4、把节点作为字符串输出

中.国.站.长.站


echo $xml->channel->item[0]->asXML(); Www@Chinaz@com

这将输出文本

Chinaz

以下为引用的内容:
<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>


Chinaz

带文件名参数的asXML将会把原本输出的内容保存为一个文件 Chinaz^com

$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>";
} Chinaz~com

echo "<hr>";
print_r($xml);
echo $xml->channel->item[0]->asXML();
?>

中.国站长站


Www@Chinaz@com

任何XML文本在输出前最好用 htmlentiteis() 函数编码后再输出,否这可能出现问题

Www~Chinaz~com

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·巧用SQL-DMO创建备份及校验的ASP应用
·ASP实现IE地址栏参数的判断
·运用ASP调用数据库中视图及存储过程
·让弹出窗口变得“听话”一些
·简单熟悉掌握ASP的语法和变量
·ASP中实现从dbf数据库导出数据到sql表
·ASP技巧:禁用页面缓存的五种方法
·所谓新云XSS跨站漏洞全公布
·ASP把长的数字用逗号隔开显示
·IIS虚拟主机网站防木马权限设置安全配置整理

最新文章
·网页设计之css+div PK table+css
·脚本控制三行三列自适应高度DIV布局
·Java 程序初始化过程详解
·Java实时应用程序中的内存管理
·如何掌握Java(J2SE篇)
·Hibernate和Jive缓存策略的比较
·Java与XML联合编程之SAX篇
·.text urlRewrite介绍
·JDOM操作XML文件
·纯CSS代码实现翻页


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号