国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP實現XML與數據格式進行轉換類實例

2024-05-04 23:38:15
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP實現XML與數據格式進行轉換類,實例分析了php進行XML格式數據的方法,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP實現XML與數據格式進行轉換類。分享給大家供大家參考。具體如下:

 

 
  1. <?php 
  2. /** 
  3. * xml2array() will convert the given XML text to an array in the XML structure.  
  4. * Link: http://www.bin-co.com/php/scripts/xml2array/  
  5. * Arguments : $contents - The XML text  
  6. * $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.  
  7. * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance. 
  8. * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.  
  9. * Examples: $array = xml2array(file_get_contents('feed.xml'));  
  10. * $array = xml2array(file_get_contents('feed.xml', 1, 'attribute')); 
  11. */ 
  12. function xml2array($contents$get_attributes = 1, $priority = 'tag') { 
  13. if (!$contentsreturn array(); 
  14. if (!function_exists('xml_parser_create')) { 
  15. // print "'xml_parser_create()' function not found!"; 
  16. return array(); 
  17. }  
  18. // Get the XML parser of PHP - PHP must have this module for the parser to work 
  19. $parser = xml_parser_create(''); 
  20. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss  
  21. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
  22. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
  23. xml_parse_into_struct($parser, trim($contents), $xml_values); 
  24. xml_parser_free($parser); 
  25. if (!$xml_valuesreturn//Hmm...  
  26. // Initializations 
  27. $xml_array = array(); 
  28. $parents = array(); 
  29. $opened_tags = array(); 
  30. $arr = array(); 
  31. $current = &$xml_array//Refference  
  32. // Go through the tags. 
  33. $repeated_tag_index = array(); //Multiple tags with same name will be turned into an array  
  34. foreach($xml_values as $data) { 
  35. unset($attributes$value); //Remove existing values, or there will be trouble  
  36. // This command will extract these variables into the foreach scope 
  37. // tag(string), type(string), level(int), attributes(array). 
  38. extract($data); //We could use the array by itself, but this cooler.  
  39. $result = array(); 
  40. $attributes_data = array(); 
  41. if (isset($value)) { 
  42. if ($priority == 'tag'$result = $value
  43. else $result['value'] = $value//Put the value in a assoc array if we are in the 'Attribute' mode  
  44. }  
  45. // Set the attributes too. 
  46. if (isset($attributesand $get_attributes) { 
  47. foreach($attributes as $attr => $val) { 
  48. if ($priority == 'tag'$attributes_data[$attr] = $val
  49. else $result['attr'][$attr] = $val//Set all the attributes in a array called 'attr'  
  50. }  
  51. }  
  52. // See tag status and do the needed. 
  53. if ($type == "open") { // The starting of the tag '<tag>' 
  54. $parent[$level-1] = &$current
  55. if (!is_array($currentor (!in_array($tagarray_keys($current)))) { // Insert New tag 
  56. $current[$tag] = $result
  57. if ($attributes_data$current[$tag . '_attr'] = $attributes_data
  58. $repeated_tag_index[$tag . '_' . $level] = 1; 
  59. $current = &$current[$tag]; 
  60. else { // There was another element with the same tag name 
  61. if (isset($current[$tag][0])) { // If there is a 0th element it is already an array 
  62. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result
  63. $repeated_tag_index[$tag . '_' . $level]++; 
  64. else { // This section will make the value an array if multiple tags with the same name appear together 
  65. $current[$tag] = array($current[$tag], $result); //This will combine the existing item and the new item together to make an array  
  66. $repeated_tag_index[$tag . '_' . $level] = 2; 
  67. if (isset($current[$tag . '_attr'])) { // The attribute of the last(0th) tag must be moved as well 
  68. $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
  69. unset($current[$tag . '_attr']); 
  70. }  
  71. }  
  72. $last_item_index = $repeated_tag_index[$tag . '_' . $level]-1; 
  73. $current = &$current[$tag][$last_item_index]; 
  74. }  
  75. elseif ($type == "complete") { // Tags that ends in 1 line '<tag />' 
  76. // See if the key is already taken. 
  77. if (!isset($current[$tag])) { // New Key 
  78. $current[$tag] = $result
  79. $repeated_tag_index[$tag . '_' . $level] = 1; 
  80. if ($priority == 'tag' and $attributes_data$current[$tag . '_attr'] = $attributes_data
  81. else { // If taken, put all things inside a list(array) 
  82. if (isset($current[$tag][0]) and is_array($current[$tag])) { // If it is already an array... 
  83. // ...push the new element into that array. 
  84. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result
  85. if ($priority == 'tag' and $get_attributes and $attributes_data) { 
  86. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data
  87. }  
  88. $repeated_tag_index[$tag . '_' . $level]++; 
  89. else { // If it is not an array... 
  90. $current[$tag] = array($current[$tag], $result); //...Make it an array using using the existing value and the new value  
  91. $repeated_tag_index[$tag . '_' . $level] = 1; 
  92. if ($priority == 'tag' and $get_attributes) { 
  93. if (isset($current[$tag . '_attr'])) { // The attribute of the last(0th) tag must be moved as well 
  94. $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
  95. unset($current[$tag . '_attr']); 
  96. }  
  97. if ($attributes_data) { 
  98. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data
  99. }  
  100. }  
  101. $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken  
  102. }  
  103. }  
  104. elseif ($type == 'close') { // End of tag '</tag>' 
  105. $current = &$parent[$level-1]; 
  106. }  
  107. }  
  108. return($xml_array); 
  109. }  
  110. // Array to XML 
  111. class array2xml { 
  112. public $output = "<?xml version=/"1.0/" encoding=/"utf-8/"?>/n"
  113. public $sub_item = array(); 
  114. public function __construct($array) { 
  115. $sub_item = array(); 
  116. $this->output .= $this->xmlmake($array); 
  117. }  
  118. public function xmlmake($array$fk = '') { 
  119. $xml = ''
  120. global $sub_item
  121. foreach ($array as $key => $value) { 
  122. if (is_array($value)) { 
  123. if (is_numeric($key)) { 
  124. $this->sub_item=array_merge($this->sub_item,array($fk)); 
  125. $xml .= "<{$fk}>" . $this->xmlmake($value$key) . "</{$fk}>"
  126. else { 
  127. $xml .= "<{$key}>" . $this->xmlmake($value$key) . "</{$key}>"
  128. }  
  129. else { 
  130. $xml .= "<{$key}>{$value}</{$key}>/n"
  131. }  
  132. }  
  133. return $xml
  134. }  
  135. public function output(){ 
  136. foreach($this->sub_item as $t){ 
  137. $this->output = str_replace("<{$t}><{$t}>","<{$t}>",$this->output); 
  138. $this->output = str_replace("</{$t}></{$t}>","</{$t}>",$this->output); 
  139. return $this->output; 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 包头市| 普兰县| 洞头县| 贵南县| 繁昌县| 兴义市| 津南区| 延川县| 芮城县| 蛟河市| 五寨县| 宜州市| 宁化县| 启东市| 乐亭县| 海口市| 武邑县| 宽甸| 丘北县| 鹿邑县| 三原县| 沛县| 大港区| 蛟河市| 视频| 四平市| 斗六市| 陇南市| 卓尼县| 图木舒克市| 合江县| 上高县| 凤庆县| 汝阳县| 泗水县| 贵定县| 宁德市| 临颍县| 成武县| 绩溪县| 靖宇县|