时间:2021-07-01 10:21:17 帮助过:44人阅读
本文实例讲述了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程序设计有所帮助。