php xml转换成数组

  1. //调用方法:
  2. $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
  3. */
  4. function xml2array($contents, $get_attributes=1, $priority = 'tag') {
  5. if(!$contents) return array();
  6. if(!function_exists('xml_parser_create')) {
  7. //print "'xml_parser_create()' function not found!";
  8. return array();
  9. }
  10. //get the xml parser of php - php must have this module for the parser to work
  11. $parser = xml_parser_create('');
  12. 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
  13. xml_parser_set_option($parser, xml_option_case_folding, 0);
  14. xml_parser_set_option($parser, xml_option_skip_white, 1);
  15. xml_parse_into_struct($parser, trim($contents), $xml_values);
  16. xml_parser_free($parser);
  17. if(!$xml_values) return;//hmm...
  18. //initializations
  19. $xml_array = array();
  20. $parents = array();
  21. $opened_tags = array();
  22. $arr = array();
  23. $current = &$xml_array; //refference
  24. //go through the tags.
  25. $repeated_tag_index = array();//multiple tags with same name will be turned into an array
  26. foreach($xml_values as $data) {
  27. unset($attributes,$value);//remove existing values, or there will be trouble
  28. //this command will extract these variables into the foreach scope
  29. // tag(string), type(string), level(int), attributes(array).
  30. extract($data);//we could use the array by itself, but this cooler.
  31. $result = array();
  32. $attributes_data = array();
  33. if(isset($value)) {
  34. if($priority == 'tag') $result = $value;
  35. else $result['value'] = $value; //put the value in a assoc array if we are in the 'attribute' mode
  36. }
  37. //set the attributes too.
  38. if(isset($attributes) and $get_attributes) {
  39. foreach($attributes as $attr => $val) {
  40. if($priority == 'tag') $attributes_data[$attr] = $val;
  41. else $result['attr'][$attr] = $val; //set all the attributes in a array called 'attr'
  42. }
  43. }
  44. //see tag status and do the needed.
  45. if($type == "open") {//the starting of the tag '<tag>'
  46. $parent[$level-1] = &$current;
  47. if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //insert new tag
  48. $current[$tag] = $result;
  49. if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
  50. $repeated_tag_index[$tag.'_'.$level] = 1;
  51. $current = &$current[$tag];
  52. } else { //there was another element with the same tag name
  53. if(isset($current[$tag][0])) {//if there is a 0th element it is already an array
  54. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  55. $repeated_tag_index[$tag.'_'.$level]++;
  56. } else {//this section will make the value an array if multiple tags with the same name appear together
  57. $current[$tag] = array($current[$tag],$result);//this will combine the existing item and the new item together to make an array
  58. $repeated_tag_index[$tag.'_'.$level] = 2;
  59. if(isset($current[$tag.'_attr'])) { //the attribute of the last(0th) tag must be moved as well
  60. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  61. unset($current[$tag.'_attr']);
  62. }
  63. }
  64. $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
  65. $current = &$current[$tag][$last_item_index];
  66. }
  67. } elseif($type == "complete") { //tags that ends in 1 line '<tag />'
  68. //see if the key is already taken.
  69. if(!isset($current[$tag])) { //new key
  70. $current[$tag] = $result;
  71. $repeated_tag_index[$tag.'_'.$level] = 1;
  72. if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
  73. } else { //if taken, put all things inside a list(array)
  74. if(isset($current[$tag][0]) and is_array($current[$tag])) {//if it is already an array...
  75. // ...push the new element into that array.
  76. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  77. if($priority == 'tag' and $get_attributes and $attributes_data) {
  78. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  79. }
  80. $repeated_tag_index[$tag.'_'.$level]++;
  81. } else { //if it is not an array...
  82. $current[$tag] = array($current[$tag],$result); //...make it an array using using the existing value and the new value
  83. $repeated_tag_index[$tag.'_'.$level] = 1;
  84. if($priority == 'tag' and $get_attributes) {
  85. if(isset($current[$tag.'_attr'])) { //the attribute of the last(0th) tag must be moved as well
  86. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  87. unset($current[$tag.'_attr']);
  88. }
  89. if($attributes_data) {
  90. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  91. }
  92. }
  93. $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
  94. }
  95. }
  96. } elseif($type == 'close') { //end of tag '</tag>'
  97. $current = &$parent[$level-1];
  98. }//开源代码phpfensi.com
  99. }
  100. return($xml_array);
  101. }
  102. #########################################################################################################
  103. class parserxml
  104. {
  105. function parserxml()
  106. {
  107. }
  108. function parserxmlbyurl($url)
  109. {
  110. $fp = @fopen($url,'r');
  111. if (!$fp){
  112. return 1;
  113. }
  114. $xmlstr = fread($fp,1024);
  115. @fclose($fp);
  116. if ($xmlstr === false){
  117. return 0;
  118. }
  119. return $this->getxmltree($xmlstr);
  120. }
  121. function getxmltree($xmldata)
  122. {
  123. ini_set ('track_errors', '1');
  124. $xmlreaderror = false;
  125. $parser = xml_parser_create();
  126. xml_parser_set_option ($parser, xml_option_skip_white, 1);
  127. xml_parser_set_option ($parser, xml_option_case_folding, 0);
  128. if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
  129. $xmlreaderror = true;
  130. return 0;
  131. }
  132. xml_parser_free($parser);
  133. if(!$xmlreaderror){
  134. $result = array ();
  135. $i = 0;
  136. if (isset($vals[$i]['attributes'])){
  137. foreach (array_keys ($vals [$i]['attributes']) as $attkey)
  138. $attributes[$attkey] = $vals[$i]['attributes'][$attkey];
  139. }
  140. $result[$vals [$i]['tag']] = array_merge((array)$attributes, $this->getchildren($vals, $i, 'open'));
  141. }
  142. ini_set('track_errors', '0');
  143. return $result;
  144. }
  145. function getchildren ($vals, &$i, $type)
  146. {
  147. if ($type == 'complete') {
  148. if (isset ($vals [$i]['value']))
  149. return ($vals [$i]['value']);
  150. else
  151. return '';
  152. }
  153. $children = array ();
  154. while ($vals [++$i]['type'] != 'close') {
  155. $type = $vals [$i]['type'];
  156. if (isset ($children [$vals [$i]['tag']])) {
  157. if (is_array ($children [$vals [$i]['tag']])) {
  158. $temp = array_keys ($children [$vals [$i]['tag']]);
  159. if (is_string ($temp [0])) {
  160. $a = $children [$vals [$i]['tag']];
  161. unset ($children [$vals [$i]['tag']]);
  162. $children [$vals [$i]['tag']][0] = $a;
  163. }
  164. } else {
  165. $a = $children [$vals [$i]['tag']];
  166. unset ($children [$vals [$i]['tag']]);
  167. $children [$vals [$i]['tag']][0] = $a;
  168. }
  169. $children [$vals [$i]['tag']][] = $this->getchildren ($vals, $i, $type);
  170. } else
  171. $children [$vals [$i]['tag']] = $this->getchildren ($vals, $i, $type);
  172. if (isset ($vals [$i]['attributes'])) {
  173. $attributes = array ();
  174. foreach (array_keys ($vals [$i]['attributes']) as $attkey)
  175. $attributes [$attkey] = $vals [$i]['attributes'][$attkey];
  176. if (isset ($children [$vals [$i]['tag']])) {
  177. if ($children [$vals [$i]['tag']] == '') {
  178. unset ($children [$vals [$i]['tag']]);
  179. $children [$vals [$i]['tag']] = $attributes;
  180. }
  181. elseif (is_array ($children [$vals [$i]['tag']])) {
  182. $index = count ($children [$vals [$i]['tag']]) - 1;
  183. if ($children [$vals [$i]['tag']][$index] == '') {
  184. unset ($children [$vals [$i]['tag']][$index]);
  185. $children [$vals [$i]['tag']][$index] = $attributes;
  186. }
  187. $children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
  188. } else {
  189. $value = $children [$vals [$i]['tag']];
  190. unset ($children [$vals [$i]['tag']]);
  191. $children [$vals [$i]['tag']]['value'] = $value;
  192. $children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
  193. }
  194. } else
  195. $children [$vals [$i]['tag']] = $attributes;
  196. }
  197. }
  198. return $children;
  199. ?>