xml解析类

转载链接:http://zyan.cc/post/253


今天在PHP4环境下重新写一个接口程序,需要大量分析解析XML,PHP的xml_parse_into_struct()函数不能直接生成便于使用的数组,而SimpleXML扩展在PHP5中才支持,于是逛逛搜索引擎,在老外的网站上找到了一个不错的PHP XML操作类。

一、用法举例:
1、将XML文件解释成便于使用的数组:

    <?php  include('xml.php');    //引用PHP XML操作类  $xml = file_get_contents('data.xml');    //读取XML文件  //$xml = file_get_contents("php://input");    //读取POST过来的输入流  $data=XML_unserialize($xml);  echo '<pre>';  print_r($data);  echo '</pre>';  ?>  

data.xml文件:
    <?xml version="1.0" encoding="GBK"?>  <video>  <upload>  <videoid>998</videoid>  <name><![CDATA[回忆未来]]></name>  <memo><![CDATA[def]]></memo>  <up_userid>11317</up_userid>  </upload>  </video>  

利用该XML操作类生成的对应数组(汉字编码:UTF-8):
    Array  (  [video] => Array  (  [upload] => Array  (  [videoid] => 998  [name] => 回忆未来  [memo] => def  [up_userid] => 11317  )  )  )  

2、将数组转换成XML文件:
    <?php  include('xml.php');//引用PHP XML操作类  $xml = XML_serialize($data);  ?>  


二、PHP XML操作类源代码:
<?php   
###################################################################################   
#   
# XML Library, by Keith Devens, version 1.2b   
# <a href="http://keithdevens.com/software/phpxml" target="_blank">http://keithdevens.com/software/phpxml</a>   
#   
# This code is Open Source, released under terms similar to the Artistic License.   
# Read the license at <a href="http://keithdevens.com/software/license" target="_blank">http://keithdevens.com/software/license</a>   
#   
###################################################################################   ###################################################################################   
# XML_unserialize: takes raw XML as a parameter (a string)   
# and returns an equivalent PHP data structure   
###################################################################################   
function & XML_unserialize(&$xml){   $xml_parser = &new XML();   $data = &$xml_parser->parse($xml);   $xml_parser->destruct();   return $data;   
}   
###################################################################################   
# XML_serialize: serializes any PHP data structure into XML   
# Takes one parameter: the data to serialize. Must be an array.   
###################################################################################   
function & XML_serialize(&$data, $level = 0, $prior_key = NULL){   if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }   while(list($key, $value) = each($data))   if(!strpos($key, ' attr')) #if it's not an attribute  #we don't treat attributes by themselves, so for an emptyempty element   # that has attributes you still need to set the element to NULL   if(is_array($value) and array_key_exists(0, $value)){   XML_serialize($value, $level, $key);   }else{   $tag = $prior_key ? $prior_key : $key;   echo str_repeat("\t", $level),'<',$tag;   if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element  while(list($attr_name, $attr_value) = each($data["$key attr"]))  echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';  reset($data["$key attr"]);  }  if(is_null($value)) echo " />\n";  elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";  else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";  }  reset($data);  if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }  
}  
###################################################################################  
# XML class: utility class to be used with PHP's XML handling functions   
###################################################################################   
class XML{   var $parser;   #a reference to the XML parser   var $document; #the entire XML structure built up so far   var $parent;   #a pointer to the current parent - the parent will be an array   var $stack;    #a stack of the most recent parent at each nesting level   var $last_opened_tag; #keeps track of the last tag opened.   function XML(){   $this->parser = &xml_parser_create();   xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);   xml_set_object(&$this->parser, &$this);   xml_set_element_handler(&$this->parser, 'open','close');   xml_set_character_data_handler(&$this->parser, 'data');   }   function destruct(){ xml_parser_free(&$this->parser); }   function & parse(&$data){   $this->document = array();   $this->stack    = array();   $this->parent   = &$this->document;   return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;   }   function open(&$parser, $tag, $attributes){   $this->data = ''; #stores temporary cdata   $this->last_opened_tag = $tag;   if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before  if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric  #this is the third or later instance of $tag we've come across   $key = count_numeric_items($this->parent[$tag]);   }else{   #this is the second instance of $tag that we've seen. shift around  if(array_key_exists("$tag attr",$this->parent)){  $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);  unset($this->parent["$tag attr"]);  }else{  $arr = array(&$this->parent[$tag]);  }  $this->parent[$tag] = &$arr;  $key = 1;  }  $this->parent = &$this->parent[$tag];  }else{  $key = $tag;  }  if($attributes) $this->parent["$key attr"] = $attributes;  $this->parent  = &$this->parent[$key];  $this->stack[] = &$this->parent;  }  function data(&$parser, $data){  if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags   $this->data .= $data;   }   function close(&$parser, $tag){   if($this->last_opened_tag == $tag){   $this->parent = $this->data;   $this->last_opened_tag = NULL;   }   array_pop($this->stack);   if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];   }   
}   
function count_numeric_items(&$array){   return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;   
}   
?>


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/276164.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

jmeter学习指南之聚合报告

jmeter视频地址&#xff1a;https://edu.51cto.com/course/14305.html 上一篇文章中我们讲了Jmeter结果分析最常用的一个Listener查看结果树&#xff0c;今天接着讲另一个最常用的listener--聚合报告Aggregate Report。我们先来看看聚合报告中的主要名称的含意&#xff1a;Labe…

敏捷开发概述

敏捷方法强调适应性而非预见性。 目前列入敏捷方法的有&#xff1a; 軟件開發節奏&#xff0c;Software Development Rhythms 敏捷數據庫技術&#xff0c;AD/Agile Database Techniques 敏捷建模&#xff0c;AM/Agile Modeling 自適應軟件開發&#xff0c;ASD/Adaptive Softwar…

2021 整理的最全学习资源,送给每一个努力着的人

时间来到了 2021 年&#xff0c;新的一年有新的期待&#xff0c;而我亦有新的祝福如果说在过去的一年&#xff0c;经历太多&#xff0c;心酸、迷茫、焦虑、幸福、喜悦那么在 2021 年&#xff0c;希望你可以去过一种遇见自己的生活&#xff0c;恬淡、热情&#xff0c;喜欢自己而…

ubuntu+php环境下的Memcached 安装方法

转载链接&#xff1a;http://www.jb51.net/article/28887.htm Memcached是一套分散式的高速缓存系统&#xff0c;当初是Danga Interactive为了LiveJournal所发展。 目前被很多系统所使用&#xff0c;例如Flick、Twitter等。这是一套开放源代码软件&#xff0c;以BSD license授…

php移动签批源码_PHP让网站移动访问更加友好方法

PHP都是在服务器上处理的&#xff0c;所以当代码到达用户时&#xff0c;它只是HTML。基本上&#xff0c;用户从你的服务器请求你网站的一个页面&#xff0c;然后你的服务器运行所有的PHP并向用户发送PHP的结果。设备实际上从未看到或必须使用实际的PHP代码。这使得使用PHP完成的…

Chrome OS 设备或将允许用户自行选择 Linux 发行版

百度智能云 云生态狂欢季 热门云产品1折起>>> 谷歌去年宣布在 Chrome OS 上支持运行 Linux 应用&#xff0c;前不久又有消息称其将为运行这些 Linux 应用提供 GPU 加速支持&#xff0c;而现在&#xff0c;Chrome OS 似乎将在 Linux 的方向上更进一步&#xff0c;让 …

博文视点 OpenParty第11期:世界黑客大会那些事

博文视点 OpenParty第11期&#xff1a;世界黑客大会那些事 亲爱的读者朋友&#xff1a; 您好&#xff01; 2009年&#xff0c;博文视点Open Party共举办8场&#xff0c;累计到场2000人次&#xff0c;影响力辐射近5000人次&#xff0c;真正实现了博文视点Open Party的初…

我从 Vuejs 中学到了什么——框架设计学问

框架设计远没有大家想的那么简单&#xff0c;并不是说只把功能开发完成&#xff0c;能用就算完事儿了&#xff0c;这里面还是有很多学问的。比如说&#xff0c;我们的框架应该给用户提供哪些构建产物&#xff1f;产物的模块格式如何&#xff1f;当用户没有以预期的方式使用框架…

CSS制作的32种图形效果[梯形|三角|椭圆|平行四边形|菱形|四分之一圆|旗帜]

转载链接&#xff1a;http://www.w3cplus.com/css/css-simple-shapes-cheat-sheet 前面在《纯CSS制作的图形效果》一文中介绍了十六种CSS画各种不同图形的方法。今天花了点时间将这方面的制作成一份清单&#xff0c;方便大家急用时有地方可查。别的不多说了&#xff0c;直接看代…

vue-cli新建的项目webpack设置涉及的大部分插件整理

portfinder 用来检测未占用的端口更多看这里: https://www.npmjs.com/package/portfinder webpack-merge 用来合并多个webpack设置&#xff0c;也可以合并对象更多看这里: https://www.npmjs.com/package/friendly-errors-webpack-plugin html-webpack-plugin 将html复制并插入…

yaml加配置文件后起不来_YAML配置文件管理资源

YAML是配置文件的格式&#xff0c;YAML文件中是由一些易读的字段和指令组成的。K8S使用YAML配置文件需要注意如下事项。定义配置时&#xff0c;指定最新稳定版API(当前最新稳定版是v1版本)。最新版本的API可以通过kubectl api-versions命令进行查看&#xff0c;命令如下所示。前…

html5/css3响应式布局介绍

转载链接&#xff1a;http://www.51xuediannao.com/htmlcss/htmlcssjq/694.html html5/css3响应式布局介绍 html5/css3响应式布局介绍及设计流程&#xff0c;利用css3的media query媒体查询功能。移动终端一般都是对css3支持比较好的高级浏览器不需要考虑响应式布局的媒体查询…

人际关系十大要诀

【一表人才】 所谓“一表人才”&#xff0c;就是说当你与陌生人第一次见面时给对方留下的第一印象&#xff0c;我们都知道第一印象很重要&#xff0c;要给对方留下好的印象&#xff0c;特别是要让对方在最短的时间记住你。那么我们自身的仪表、行为举止都很重要&#xff1b;我们…

MobX 上手指南,写 Vue 的感觉?

之前用 Redux 比较多&#xff0c;一直听说 Mobx 能让你体验到在 React 里面写 Vue 的感觉&#xff0c;今天打算尝试下 Mobx 是不是真的有写 Vue 的感觉。题外话在介绍 MobX 的用法之前&#xff0c;先说点题外话&#xff0c;我们可以看一下 MobX 的中文简介。在 MobX 的中文网站…

ansible中yaml语法应用

4、yaml语法应用 ansible的playbook编写是yaml语言编写&#xff0c;掌握yaml语法是编写playbook的必要条件&#xff0c;格式要求和Python相似&#xff0c;具体教程参考如下yaml语言教程 附上一个yaml文件转js格式文件链接在线免费yaml内容转json格式 4.1、 ansible中的yaml语法…

中兴a2018拆机图片_中兴天机拆机步骤详解【图文】

中兴天机上市时有两款&#xff0c;黑色和白色。黑色的缺点是外观过于传统&#xff0c;并不是很适合年轻人使用&#xff0c;但是其推出白色款却很好的解决了这个问题。中兴天机的整体性质与性价比完美的拼过了 小米 3等同时上线的手机产品。中兴天机价格在1799左右&#xff0c;小…

网络视频贴片广告全面推行第三方监测

视频网站优酷与国际调研机构尼尔森联合对外宣布&#xff1a;针对优酷视频贴片广告全面推行第三方监测。这是视频行业首次倡导广告投放数据透明化的一大举措。  近年来&#xff0c;网络视频已经成为广告主营销的一大选择。随着广告主投放额度不断加大&#xff0c;广告主对视频…

css3动画事件—webkitAnimationEnd

转载链接&#xff1a;http://www.jb51.net/css/72443.html 用css3的animation完成一个动画&#xff0c;当只有这个动画完成时才执行令一个事件&#xff0c;比如让动画保持在终止的状态或其他一些事件。我们该怎么办呢。 第一种方法&#xff1a; 用计时器&#xff0c;设定一个…

(送书和红包)快人一步,掌握前端函数式编程

大家好&#xff0c;我是若川。上周末送出了3本新书和若干红包&#xff0c;抽奖名单已公布。本周又争取到了4本《前端函数式编程》书籍包邮送给大家&#xff0c;抽奖规则见文末&#xff0c;与以往不同的是除了关键词、留言、在看抽奖外&#xff0c;还有最早关注奖&#xff0c;欢…

js split参数为无效字符_js使用split函数按照多个字符对字符串进行分割的方法

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":5,"count":5}]},"card":[{"des":"阿里云函数计算(Function Compute)是一个事件…