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

yipeiwu_com6年前PHP代码库

本文实例讲述了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下载文件源代码(强制任意文件格式下载)

一个简单的php文件下载源代码,虽不支持断点续传等,但是可以满足一些常用的需求了。php下载文件其实用一个a标签就能实现,比如 <a href="web/magento-1.8.1...

IIS+fastcgi下PHP运行超时问题的解决办法详解

每在页面中上传较大的文件时遇到FastCgi的错误:“The FastCGI process exceeded configured request timeout”,几经试验,明白了需...

php+jQuery实现的三级导航栏下拉菜单显示效果

php+jQuery实现的三级导航栏下拉菜单显示效果

本文实例讲述了php+jQuery实现的三级导航栏下拉菜单显示效果。分享给大家供大家参考,具体如下: 首先看看效果图: 1.数据配置文件 db.php <?php r...

PHP在字符串中查找指定字符串并删除的代码

$a = "abcababa"; $count=strpos($a,"ab"); $str=substr_replace($a,"",$count,2); 输出结果:cababa 代码虽...

php二分法在IP地址查询中的应用

数据库大概存储几十万条IP记录,记录集如下: +----------+----------+------------+---------+---------+--------+-----...