免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2039 | 回复: 1
打印 上一主题 下一主题

PHP XML Library:一个不错的PHP XML操作类 (接上) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-08 17:29 |只看该作者 |倒序浏览
PHP XML Library:一个不错的PHP XML操作类 (接上)..
  1. ###################################################################################
  2. #
  3. # XML Library, by Keith Devens, version 1.2b
  4. # <a href="http://keithdevens.com/software/phpxml" target="_blank">http://keithdevens.com/software/phpxml</a>
  5. #
  6. # This code is Open Source, released under terms similar to the Artistic License.
  7. # Read the license at <a href="http://keithdevens.com/software/license" target="_blank">http://keithdevens.com/software/license</a>
  8. #
  9. ###################################################################################

  10. ###################################################################################
  11. # XML_unserialize: takes raw XML as a parameter (a string)
  12. # and returns an equivalent PHP data structure
  13. ###################################################################################
  14. function & XML_unserialize(&$xml){
  15.     $xml_parser = &new XML();
  16.     $data = &$xml_parser->parse($xml);
  17.     $xml_parser->destruct();
  18.     return $data;
  19. }
  20. ###################################################################################
  21. # XML_serialize: serializes any PHP data structure into XML
  22. # Takes one parameter: the data to serialize. Must be an array.
  23. ###################################################################################
  24. function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
  25.     if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
  26.     while(list($key, $value) = each($data))
  27.         if(!strpos($key, ' attr')) #if it's not an attribute
  28.             #we don't treat attributes by themselves, so for an empty element
  29.             # that has attributes you still need to set the element to NULL

  30.             if(is_array($value) and array_key_exists(0, $value)){
  31.                 XML_serialize($value, $level, $key);
  32.             }else{
  33.                 $tag = $prior_key ? $prior_key : $key;
  34.                 echo str_repeat("\t", $level),'<',$tag;
  35.                 if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
  36.                     while(list($attr_name, $attr_value) = each($data["$key attr"]))
  37.                         echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
  38.                     reset($data["$key attr"]);
  39.                 }

  40.                 if(is_null($value)) echo " />\n";
  41.                 elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
  42.                 else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
  43.             }
  44.     reset($data);
  45.     if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
  46. }
  47. ###################################################################################
  48. # XML class: utility class to be used with PHP's XML handling functions
  49. ###################################################################################

  50. class XML{
  51.     var $parser;   #a reference to the XML parser
  52.     var $document; #the entire XML structure built up so far
  53.     var $parent;   #a pointer to the current parent - the parent will be an array
  54.     var $stack;    #a stack of the most recent parent at each nesting level
  55.     var $last_opened_tag; #keeps track of the last tag opened.

  56.     function XML(){
  57.          $this->parser = &xml_parser_create();
  58.         xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);
  59.         xml_set_object(&$this->parser, &$this);
  60.         xml_set_element_handler(&$this->parser, 'open','close');
  61.         xml_set_character_data_handler(&$this->parser, 'data');
  62.     }
  63.     function destruct(){ xml_parser_free(&$this->parser); }
  64.     function & parse(&$data){
  65.         $this->document = array();
  66.         $this->stack    = array();
  67.         $this->parent   = &$this->document;
  68.         return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;
  69.     }
  70.     function open(&$parser, $tag, $attributes){
  71.         $this->data = ''; #stores temporary cdata
  72.         $this->last_opened_tag = $tag;
  73.         if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
  74.             if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
  75.                 #this is the third or later instance of $tag we've come across
  76.                 $key = count_numeric_items($this->parent[$tag]);
  77.             }else{
  78.                 #this is the second instance of $tag that we've seen. shift around
  79.                 if(array_key_exists("$tag attr",$this->parent)){
  80.                     $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
  81.                     unset($this->parent["$tag attr"]);
  82.                 }else{
  83.                     $arr = array(&$this->parent[$tag]);
  84.                 }
  85.                 $this->parent[$tag] = &$arr;
  86.                 $key = 1;
  87.             }
  88.             $this->parent = &$this->parent[$tag];
  89.         }else{
  90.             $key = $tag;
  91.         }
  92.         if($attributes) $this->parent["$key attr"] = $attributes;
  93.         $this->parent  = &$this->parent[$key];
  94.         $this->stack[] = &$this->parent;
  95.     }
  96.     function data(&$parser, $data){
  97.         if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
  98.             $this->data .= $data;
  99.     }
  100.     function close(&$parser, $tag){
  101.         if($this->last_opened_tag == $tag){
  102.             $this->parent = $this->data;
  103.             $this->last_opened_tag = NULL;
  104.         }
  105.         array_pop($this->stack);
  106.         if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
  107.     }
  108. }
  109. function count_numeric_items(&$array){
  110.     return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
  111. }
  112. ?>
复制代码
[/code]

论坛徽章:
0
2 [报告]
发表于 2011-11-14 09:57 |只看该作者
有爱楼主..
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP