亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > 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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲xxxx视频| 国产suv精品一区二区三区88区| 久久亚洲精品国产亚洲老地址| 精品网站999www| 亚洲一区二区久久久久久久| 欧美成人精品xxx| 国产玖玖精品视频| 九色精品美女在线| 欧美性猛xxx| 国产精品女人久久久久久| 91精品啪aⅴ在线观看国产| 性色av香蕉一区二区| 青青草国产精品一区二区| 98午夜经典影视| 精品成人久久av| 欧美性生交大片免网| 色999日韩欧美国产| 日韩精品视频在线观看网址| 国产精品美女www| 日韩精品中文字幕视频在线| 91国产精品视频在线| 欧美成人一区在线| 一区二区在线免费视频| 欧美日韩精品在线| 亚洲伊人久久综合| 国产精品jvid在线观看蜜臀| 国内揄拍国内精品| 国产91免费看片| 欧美在线一级va免费观看| 欧美激情视频在线观看| 欧美又大粗又爽又黄大片视频| 欧美日韩国产91| 91色在线视频| 欧美做受高潮电影o| 国产成人精品一区二区三区| 日韩在线观看免费全| 欧美日韩在线另类| 热re91久久精品国99热蜜臀| 国产一区二区三区在线免费观看| 亚洲最大福利视频网站| 国产精品美女久久久久av超清| 成人日韩在线电影| 国产午夜精品美女视频明星a级| 国产在线视频不卡| 欧美精品久久一区二区| 欧美另类精品xxxx孕妇| 黑人极品videos精品欧美裸| 欧美在线视频观看| 日韩美女免费线视频| 中文日韩电影网站| 日韩在线精品视频| 搡老女人一区二区三区视频tv| 亚洲奶大毛多的老太婆| 国产色婷婷国产综合在线理论片a| 国产精品美女999| 最近中文字幕日韩精品| 91网站在线免费观看| 国产精品第1页| 精品日本美女福利在线观看| 亚洲欧洲日产国码av系列天堂| 中文字幕精品国产| 日韩av片永久免费网站| 久久亚洲一区二区三区四区五区高| 亚洲自拍小视频| 久久久久亚洲精品成人网小说| 欧美黑人巨大精品一区二区| 欧美日韩免费在线| 正在播放欧美视频| 亚洲成av人影院在线观看| 国产一区二区欧美日韩| 久久久国产精彩视频美女艺术照福利| 国产亚洲日本欧美韩国| 亚洲视频欧美视频| 久久亚洲一区二区三区四区五区高| 91精品国产乱码久久久久久蜜臀| 日韩成人激情影院| 亚洲欧美日韩一区二区在线| 日韩美女视频在线观看| 欧美一级成年大片在线观看| 日韩av资源在线播放| 亚洲精品不卡在线| 亚洲色图第一页| 欧美极品在线视频| 国产精品∨欧美精品v日韩精品| 一二美女精品欧洲| 国产成人精品电影久久久| 美日韩精品免费观看视频| 日韩有码视频在线| 久久精品国产成人精品| 视频直播国产精品| 亚洲天堂av高清| 一区二区三区无码高清视频| 精品高清一区二区三区| 亚洲国模精品一区| 免费99精品国产自在在线| 久久久久久九九九| 蜜臀久久99精品久久久久久宅男| 在线观看日韩www视频免费| 亚洲欧美成人精品| 精品亚洲国产成av人片传媒| 97精品欧美一区二区三区| 成人久久精品视频| 中国日韩欧美久久久久久久久| 不卡av在线播放| 亚洲一级黄色av| 69久久夜色精品国产7777| 久久免费高清视频| 国产午夜精品一区理论片飘花| 日韩av在线导航| 欧美日韩一二三四五区| 97在线免费视频| 国产成人拍精品视频午夜网站| 国产日韩欧美视频| 成人福利视频在线观看| 国产欧美日韩最新| 日韩激情片免费| 精品小视频在线| 91av网站在线播放| 精品香蕉在线观看视频一| 国产91色在线|| 亚洲午夜精品久久久久久久久久久久| 久久精品国产96久久久香蕉| 色播久久人人爽人人爽人人片视av| 久久久国产精彩视频美女艺术照福利| 国产精品最新在线观看| 国产精品激情av电影在线观看| 日韩在线免费av| 国产精品高潮呻吟久久av黑人| 成人性生交大片免费观看嘿嘿视频| 国产一区二区黄| 久久精品一本久久99精品| 色婷婷av一区二区三区久久| 久久精品成人动漫| 91沈先生作品| 欧美视频在线观看免费网址| 狠狠色香婷婷久久亚洲精品| 亚洲天堂网在线观看| 日本午夜精品理论片a级appf发布| 久久精品久久久久久国产 免费| 久久综合久久八八| 国产欧美日韩最新| 精品国偷自产在线视频99| 欧美日韩国产精品一区| 最近2019中文字幕第三页视频| 亚洲色图激情小说| 在线观看久久久久久| www国产精品视频| 欧美视频在线视频| 亚洲国产一区二区三区四区| 欧美成人午夜视频| 欧美成人在线影院| 欧美在线视频网站| 日韩激情视频在线| 亚洲成年人影院在线| 国产原创欧美精品| 亚洲国产成人精品久久久国产成人一区| 奇米4444一区二区三区| 日本乱人伦a精品| 免费av一区二区| 亚洲精品国产免费| 久久这里有精品| 福利精品视频在线| 日韩欧美国产中文字幕| 国产不卡视频在线|