PHP简单读取xml文件的方法示例

所属分类: 网络编程 / PHP编程 阅读数: 677
收藏 0 赞 0 分享

本文实例讲述了PHP简单读取xml文件的方法。分享给大家供大家参考,具体如下:

我将软件版本更新中的版本号等数据信息存放在xml文件中,使用时将版本信息读取出来。

xml文件内容如下:

<xml version="v1.01" encoding="utf-8">
 <updataMessages>
<version>v1.8.7</version>
 </updataMessages>
</xml>

下面是PHP如何读取xml文件

$doc = new DOMDocument();
$filepath=$_SERVER['DOCUMENT_ROOT']."/upload/versionpc/ios.xml"; //xml文件路径
$doc->load($filepath);
$books = $doc->getElementsByTagName("updataMessages");
foreach( $books as $book )
{
$versions = $book->getElementsByTagName("version");
$version = $versions->item(0)->nodeValue;
$newmsgs = $book->getElementsByTagName("newmsg");
$newmsg = $newmsgs->item(0)->nodeValue;
if($version2==$version)
{
$return = array(
"status"=>0,
"msg"=>"success"
);
}
else
{
$return = array(
"status"=>2,
"msg"=>"have new version",
"data"=>$newmsg
);
}
}

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

在线格式化XML/在线压缩XML
http://tools.jb51.net/code/xmlformat

XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

更多精彩内容其他人还在看

用php实现像JSP,ASP里Application那样的全局变量

用php实现像JSP,ASP里Application那样的全局变量
收藏 0 赞 0 分享

自动分页的不完整解决方案

自动分页的不完整解决方案
收藏 0 赞 0 分享

FCKeditor的安装(PHP)

FCKeditor的安装(PHP)
收藏 0 赞 0 分享

mysql5详细安装教程

mysql5详细安装教程
收藏 0 赞 0 分享

isset和empty的区别

isset和empty的区别
收藏 0 赞 0 分享

php5.2时间相差8小时

php5.2时间相差8小时
收藏 0 赞 0 分享

安装APACHE

安装APACHE
收藏 0 赞 0 分享

PHP5 安装方法

PHP5 安装方法
收藏 0 赞 0 分享

PHP has encountered an Access Violation

PHP has encountered an Access Violation
收藏 0 赞 0 分享

MYSQL环境变量设置方法

本文介绍了mysql数据库中环境变量的设置方法,如何设置mysql数据库的环境变量,有需要的朋友参考下
收藏 0 赞 0 分享
查看更多