当前位置:Gxlcms > PHP教程 > PHP简单读取xml文件的方法示例

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

时间:2021-07-01 10:21:17 帮助过:44人阅读

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

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

xml文件内容如下:

  1. <xml version="v1.01" encoding="utf-8">
  2. <updataMessages>
  3. <version>v1.8.7</version>
  4. </updataMessages>
  5. </xml>

下面是PHP如何读取xml文件

  1. $doc = new DOMDocument();
  2. $filepath=$_SERVER['DOCUMENT_ROOT']."/upload/versionpc/ios.xml"; //xml文件路径
  3. $doc->load($filepath);
  4. $books = $doc->getElementsByTagName("updataMessages");
  5. foreach( $books as $book )
  6. {
  7. $versions = $book->getElementsByTagName("version");
  8. $version = $versions->item(0)->nodeValue;
  9. $newmsgs = $book->getElementsByTagName("newmsg");
  10. $newmsg = $newmsgs->item(0)->nodeValue;
  11. if($version2==$version)
  12. {
  13. $return = array(
  14. "status"=>0,
  15. "msg"=>"success"
  16. );
  17. }
  18. else
  19. {
  20. $return = array(
  21. "status"=>2,
  22. "msg"=>"have new version",
  23. "data"=>$newmsg
  24. );
  25. }
  26. }

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程序设计有所帮助。

人气教程排行