Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2060955
  • 博文数量: 178
  • 博客积分: 2076
  • 博客等级: 大尉
  • 技术积分: 2800
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-10 10:50
文章分类

全部博文(178)

文章存档

2010年(4)

2009年(13)

2008年(161)

我的朋友

分类: LINUX

2008-10-23 16:24:40

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

一、用法举例:
1、将XML文件解释成便于使用的数组:
  1. include('xml.php');    //引用PHP XML操作类   
  2. $xml = file_get_contents('data.xml');    //读取XML文件   
  3. //$xml = file_get_contents("php://input");    //读取POST过来的输入流   
  4. $data=XML_unserialize($xml);   
  5. echo '
    ';   
  6. print_r($data);   
  7. echo '';   
  8. ?>  


data.xml文件:
  1. xml version="1.0" encoding="GBK"?>  
  2. <video>  
  3. <upload>  
  4. <videoid>998videoid>  
  5. <name>name>  
  6. <memo>memo>  
  7. <up_userid>11317up_userid>  
  8. upload>  
  9. video>  


利用该XML操作类生成的对应数组(汉字编码:UTF-8):
  1. Array   
  2. (   
  3.     [video] => Array   
  4.         (   
  5.             [upload] => Array   
  6.                 (   
  7.                     [videoid] => 998   
  8.                     [name] => 回忆未来   
  9.                     [memo] => def   
  10.                     [up_userid] => 11317   
  11.                 )   
  12.   
  13.         )   
  14.   
  15. )  


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


二、PHP XML操作类源代码:
  1. ###################################################################################    
  2. #    
  3. # XML Library, by Keith Devens, version 1.2b    
  4. "http://keithdevens.com/software/phpxml" target="_blank">http://keithdevens.com/software/phpxml    
  5. #    
  6. # This code is Open Source, released under terms similar to the Artistic License.    
  7. # Read the license at "http://keithdevens.com/software/license" target="_blank">http://keithdevens.com/software/license    
  8. #    
  9. ###################################################################################    
  10.   
  11. ###################################################################################    
  12. # XML_unserialize: takes raw XML as a parameter (a string)    
  13. and returns an equivalent PHP data structure    
  14. ###################################################################################    
  15. function & XML_unserialize(&$xml){    
  16.     $xml_parser = &new XML();    
  17.     $data = &$xml_parser->parse($xml);    
  18.     $xml_parser->destruct();    
  19.     return $data;    
  20. }    
  21. ###################################################################################    
  22. # XML_serialize: serializes any PHP data structure into XML    
  23. # Takes one parameter: the data to serialize. Must be an array.    
  24. ###################################################################################    
  25. function & XML_serialize(&$data$level = 0, $prior_key = NULL){    
  26.     if($level == 0){ ob_start(); echo '',"\n"; }    
  27.     while(list($key$value) = each($data))    
  28.         if(!strpos($key' attr')) #if it's not an attribute   
  29.             #we don't treat attributes by themselves, so for an emptyempty element    
  30.             # that has attributes you still need to set the element to NULL    
  31.   
  32.             if(is_array($valueand array_key_exists(0, $value)){    
  33.                 XML_serialize($value$level$key);    
  34.             }else{    
  35.                 $tag = $prior_key ? $prior_key : $key;    
  36.                 echo str_repeat("\t"$level),'<',$tag;    
  37.                 if(array_key_exists("$key attr"$data)){ #if there's an attribute for this element   
  38.                     while(list($attr_name, $attr_value) = each($data["$key attr"]))   
  39.                         echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';   
  40.                     reset($data["$key attr"]);   
  41.                 }   
  42.                 if(is_null($value)) echo " />\n";   
  43.                 elseif(!is_array($value)) echo '>',htmlspecialchars($value),"\n";   
  44.                 else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"\n";   
  45.             }   
  46.     reset($data);   
  47.     if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }   
  48.  
  49. ###################################################################################   
  50. # XML class: utility class to be used with PHP's XML handling functions    
  51. ###################################################################################    
  52. class XML{    
  53.     var $parser;   #a reference to the XML parser    
  54.     var $document; #the entire XML structure built up so far    
  55.     var $parent;   #a pointer to the current parent - the parent will be an array    
  56.     var $stack;    #a stack of the most recent parent at each nesting level    
  57.     var $last_opened_tag; #keeps track of the last tag opened.    
  58.   
  59.     function XML(){    
  60.          $this->parser = &xml_parser_create();    
  61.         xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);    
  62.         xml_set_object(&$this->parser, &$this);    
  63.         xml_set_element_handler(&$this->parser, 'open','close');    
  64.         xml_set_character_data_handler(&$this->parser, 'data');    
  65.     }    
  66.     function destruct(){ xml_parser_free(&$this->parser); }    
  67.     function & parse(&$data){    
  68.         $this->document = array();    
  69.         $this->stack    = array();    
  70.         $this->parent   = &$this->document;    
  71.         return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;    
  72.     }    
  73.     function open(&$parser$tag$attributes){    
  74.         $this->data = ''; #stores temporary cdata    
  75.         $this->last_opened_tag = $tag;    
  76.         if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before   
  77.             if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric   
  78.                 #this is the third or later instance of $tag we've come across    
  79.                 $key = count_numeric_items($this->parent[$tag]);    
  80.             }else{    
  81.                 #this is the second instance of $tag that we've seen. shift around   
  82.                 if(array_key_exists("$tag attr",$this->parent)){   
  83.                     $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);   
  84.                     unset($this->parent["$tag attr"]);   
  85.                 }else{   
  86.                     $arr = array(&$this->parent[$tag]);   
  87.                 }   
  88.                 $this->parent[$tag] = &$arr;   
  89.                 $key = 1;   
  90.             }   
  91.             $this->parent = &$this->parent[$tag];   
  92.         }else{   
  93.             $key = $tag;   
  94.         }   
  95.         if($attributes) $this->parent["$key attr"] = $attributes;   
  96.         $this->parent  = &$this->parent[$key];   
  97.         $this->stack[] = &$this->parent;   
  98.     }   
  99.     function data(&$parser, $data){   
  100.         if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags    
  101.             $this->data .= $data;    
  102.     }    
  103.     function close(&$parser$tag){    
  104.         if($this->last_opened_tag == $tag){    
  105.             $this->parent = $this->data;    
  106.             $this->last_opened_tag = NULL;    
  107.         }    
  108.         array_pop($this->stack);    
  109.         if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];    
  110.     }    
  111. }    
  112. function count_numeric_items(&$array){    
  113.     return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;    
  114. }    
  115. ?>    
阅读(680) | 评论(0) | 转发(0) |
0

上一篇:NOTEBOOK hp

下一篇:unix体验中心 xiaoone mylove

给主人留下些什么吧!~~