源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

php读取XML的常见方法实例总结

  • 时间:2022-04-06 10:04 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php读取XML的常见方法实例总结
本文实例讲述了php读取XML的常见方法。分享给大家供大家参考,具体如下: xml源文件
<?xml version="1.0 encoding="UTF-8"?>
<humans>
   <zhangying>
     <name>张映</name>
     <sex>男</sex>
     <old>28</old>
   </zhangying>
   <tank>
     <name>tank</name>
     <sex>男</sex>
     <old>28</old>
   </tank>
</humans>

1)[b]DOMDocument[/b]读取xml
<?php
   $doc = new DOMDocument();
   $doc->load('person.xml'); //读取xml文件
   $humans = $doc->getElementsByTagName( "humans" ); //取得humans标签的对象数组
   foreach( $humans as $human )
   {
     $names = $human->getElementsByTagName( "name" ); //取得name的标签的对象数组
     $name = $names->item(0)->nodeValue; //取得node中的值,如<name> </name>
     $sexs = $human->getElementsByTagName( "sex" );
     $sex = $sexs->item(0)->nodeValue;
     $olds = $human->getElementsByTagName( "old" );
     $old = $olds->item(0)->nodeValue;
     echo "$name - $sex - $old\n";
   }
?>

2)[b]simplexml[/b]读取xml
<?php
   $xml_array=simplexml_load_file('person.xml'); //将XML中的数据,读取到数组对象中
   foreach($xml_array as $tmp){
     echo $tmp->name."-".$tmp->sex."-".$tmp->old."<br>";
   }
?>

3)用php[b]正则表达式[/b]来读取数据
<?php
   $xml = "";
   $f = fopen('person.xml', 'r');
   while( $data = fread( $f, 4096 ) ) {
     $xml .= $data;
   }
   fclose( $f );
   // 上面读取数据
   preg_match_all( "/\<humans\>(.*?)\<\/humans\>/s", $xml, $humans ); //匹配最外层标签里面的内容
   foreach( $humans[1] as $k=>$human )
   {
     preg_match_all( "/\<name\>(.*?)\<\/name\>/", $human, $name ); //匹配出名字
     preg_match_all( "/\<sex\>(.*?)\<\/sex\>/", $human, $sex ); //匹配出性别
     preg_match_all( "/\<old\>(.*?)\<\/old\>/", $human, $old ); //匹配出年龄
   }
   foreach($name[1] as $key=>$val){
     echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."<br>" ;
   }
?>

4)[b]xmlreader[/b]来读取xml数据
<?php
   $reader = new XMLReader();
   $reader->open('person.xml'); //读取xml数据
   $i=1;
   while ($reader->read()) { //是否读取
     if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
       if($i%3) {
         echo $reader->value; //取得node的值
       } else {
         echo $reader->value."<br>" ;
       }
       $i++;
     }
   }
?>

[b]PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:[/b] [b]在线XML/JSON互相转换工具: [/b][url=http://tools.jb51.net/code/xmljson]http://tools.jb51.net/code/xmljson[/url] [b]在线格式化XML/在线压缩XML: [/b][url=http://tools.jb51.net/code/xmlformat]http://tools.jb51.net/code/xmlformat[/url] [b]XML[/b][b]在线压缩/格式化工具: [/b][url=http://tools.jb51.net/code/xml_format_compress]http://tools.jb51.net/code/xml_format_compress[/url] [b]XML[/b][b]代码在线格式化美化工具: [/b][url=http://tools.jb51.net/code/xmlcodeformat]http://tools.jb51.net/code/xmlcodeformat[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/234.htm]PHP针对XML文件操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/196.htm]PHP错误与异常处理方法总结[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部