PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/libraries/xml.class.php

https://github.com/sahilbabu/phpb2b
PHP | 122 lines | 94 code | 4 blank | 24 comment | 19 complexity | 5811f263a5e10f9d90fdd0fd0831c08b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. ###################################################################################
  3. #
  4. # XML Library, by Keith Devens, version 1.2b
  5. # http://keithdevens.com/software/phpxml
  6. #
  7. # This code is Open Source, released under terms similar to the Artistic License.
  8. # Read the license at http://keithdevens.com/software/license
  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 '<?xml version="1.0" ?>',"\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 empty element
  30. # that has attributes you still need to set the element to NULL
  31. if(is_array($value) and array_key_exists(0, $value)){
  32. XML_serialize($value, $level, $key);
  33. }else{
  34. $tag = $prior_key ? $prior_key : $key;
  35. echo str_repeat("\t", $level),'<',$tag;
  36. if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
  37. while(list($attr_name, $attr_value) = each($data["$key attr"]))
  38. echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
  39. reset($data["$key attr"]);
  40. }
  41. if(is_null($value)) echo " />\n";
  42. elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
  43. else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
  44. }
  45. reset($data);
  46. if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
  47. }
  48. ###################################################################################
  49. # XML class: utility class to be used with PHP's XML handling functions
  50. ###################################################################################
  51. class XML{
  52. var $parser; #a reference to the XML parser
  53. var $document; #the entire XML structure built up so far
  54. var $parent; #a pointer to the current parent - the parent will be an array
  55. var $stack; #a stack of the most recent parent at each nesting level
  56. var $last_opened_tag; #keeps track of the last tag opened.
  57. function XML(){
  58. $this->parser = &xml_parser_create();
  59. xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);
  60. xml_set_object(&$this->parser, &$this);
  61. xml_set_element_handler(&$this->parser, 'open','close');
  62. xml_set_character_data_handler(&$this->parser, 'data');
  63. }
  64. function destruct(){ xml_parser_free(&$this->parser); }
  65. function & parse(&$data){
  66. $this->document = array();
  67. $this->stack = array();
  68. $this->parent = &$this->document;
  69. $return = xml_parse(&$this->parser, &$data, true);
  70. if ($return) {
  71. return $this->document;
  72. }else{
  73. return NULL;
  74. }
  75. }
  76. function open(&$parser, $tag, $attributes){
  77. $this->data = ''; #stores temporary cdata
  78. $this->last_opened_tag = $tag;
  79. if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
  80. if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
  81. #this is the third or later instance of $tag we've come across
  82. $key = count_numeric_items($this->parent[$tag]);
  83. }else{
  84. #this is the second instance of $tag that we've seen. shift around
  85. if(array_key_exists("$tag attr",$this->parent)){
  86. $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
  87. unset($this->parent["$tag attr"]);
  88. }else{
  89. $arr = array(&$this->parent[$tag]);
  90. }
  91. $this->parent[$tag] = &$arr;
  92. $key = 1;
  93. }
  94. $this->parent = &$this->parent[$tag];
  95. }else{
  96. $key = $tag;
  97. }
  98. if($attributes) $this->parent["$key attr"] = $attributes;
  99. $this->parent = &$this->parent[$key];
  100. $this->stack[] = &$this->parent;
  101. }
  102. function data(&$parser, $data){
  103. if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
  104. $this->data .= $data;
  105. }
  106. function close(&$parser, $tag){
  107. if($this->last_opened_tag == $tag){
  108. $this->parent = $this->data;
  109. $this->last_opened_tag = NULL;
  110. }
  111. array_pop($this->stack);
  112. if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
  113. }
  114. }
  115. function count_numeric_items(&$array){
  116. return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
  117. }
  118. ?>