当前位置:Gxlcms > PHP教程 > PHP实现的XML操作类【XMLLibrary】

PHP实现的XML操作类【XMLLibrary】

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

本文实例讲述了PHP实现的XML操作类。分享给大家供大家参考,具体如下:

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

一、用法举例:

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

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

data.xml文件:

  1. <?xml version="1.0" encoding="GBK"?>
  2. <video>
  3. <upload>
  4. <videoid>998</videoid>
  5. <name><![CDATA[回忆未来]]></name>
  6. <memo><![CDATA[def]]></memo>
  7. <up_userid>11317</up_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. )

2、将数组转换成XML文件:

  1. <?php
  2. include('xml.php');//引用PHP XML操作类
  3. $xml = XML_serialize($data);
  4. ?>

二、PHP XML操作类源代码:

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

希望本文所述对大家PHP程序设计有所帮助。

更多PHP实现的XML操作类【XML Library】相关文章请关注PHP中文网!

人气教程排行