Guest User

Untitled

a guest
May 4th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.69 KB | None | 0 0
  1. function xml2array($contents, $get_attributes=1) {
  2.     if(!$contents) return array();
  3.     if(!function_exists('xml_parser_create')) { return array(); }
  4.     $parser = xml_parser_create();
  5.     xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
  6.     xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
  7.     xml_parse_into_struct( $parser, $contents, $xml_values );
  8.     xml_parser_free( $parser );
  9.     if(!$xml_values) return; // Hmm...
  10.     $xml_array = array();
  11.     $parents = array();
  12.     $opened_tags = array();
  13.     $arr = array();
  14.     $current = &$xml_array;
  15.     foreach($xml_values as $data) {
  16.         unset($attributes,$value);
  17.         extract($data);
  18.         $result = '';
  19.         if($get_attributes) {//The second argument of the function decides this.
  20.             $result = array();
  21.             if(isset($value)) $result['value'] = $value;
  22.             if(isset($attributes)) {
  23.                 foreach($attributes as $attr => $val) {
  24.                     if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  25.                 }
  26.             }
  27.         } elseif(isset($value)) { $result = $value; }
  28.         if($type == "open") {//The starting of the tag '<tag>'
  29.             $parent[$level-1] = &$current;
  30.             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  31.                 $current[$tag] = $result;
  32.                 $current = &$current[$tag];
  33.             } else {
  34.                 if(isset($current[$tag][0])) { array_push($current[$tag], $result);
  35.                 } else { $current[$tag] = array($current[$tag],$result); }
  36.                 $last = count($current[$tag]) - 1;
  37.                 $current = &$current[$tag][$last];
  38.             }
  39.         } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
  40.             if(!isset($current[$tag])) { //New Key
  41.                 $current[$tag] = $result;
  42.             } else { //If taken, put all things inside a list(array)
  43.                 if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
  44.                 or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) { array_push($current[$tag],$result);
  45.                 } else { $current[$tag] = array($current[$tag],$result); }
  46.             }
  47.         } elseif($type == 'close') { $current = &$parent[$level-1]; }
  48.     }
  49.     return($xml_array);
  50. }
  51.  
  52.  
  53.         $xml_tags = $all_xml_tags = array();
  54.         $new_data_key = $data_num_key = false;
  55.         $xml_data = xml2array($xml_contents,0);
  56.  
  57.         foreach($xml_data as $main_xml_value => $xml_general_data) {
  58.             if (is_array($xml_general_data) && !empty($xml_general_data)) {
  59.                 foreach($xml_general_data as $xml_specific_key => $xml_specific_data) {
  60.                     if (is_array($xml_specific_data) && !empty($xml_specific_data)) {
  61.                         foreach($xml_specific_data as $data_num_key => $data_num_value) {
  62.                             if (is_array($data_num_value) && !empty($data_num_value)) {
  63.                                 foreach($data_num_value as $new_data_key => $new_data_value) {
  64.                                     if (is_array($new_data_value) && !empty($new_data_value)) {
  65.                                         foreach($new_data_value as $new_data_xml_key => $new_data_xml_value) {
  66.                                             if (!in_array($new_data_xml_key, $xml_tags) && !is_numeric($new_data_xml_key)) {
  67.                                                 $xml_tags[] = $new_data_xml_key;
  68.                                                 if (is_numeric($new_data_key)) {
  69.                                                     $new_data_key = $data_num_key;
  70.                                                 }
  71.                                                 $all_xml_tags[] = array('path' => $main_xml_value.'-&gt;'.$xml_specific_key.'-&gt;'.$new_data_key.'-&gt;'.$new_data_xml_key, 'xml_tag' => $new_data_xml_key);
  72.                                             }
  73.                                         }
  74.                                     }
  75.                                     else {
  76.                                         if (!in_array($new_data_key, $xml_tags) && !is_numeric($new_data_key)) {
  77.                                             $xml_tags[] = $new_data_key;
  78.                                             $all_xml_tags[] = array('path' => $main_xml_value.'-&gt;'.$xml_specific_key.'-&gt;'.$new_data_key, 'xml_tag' => $new_data_key);
  79.                                         }
  80.                                     }
  81.                                 }
  82.                             }
  83.                         }
  84.                     }
  85.                 }
  86.             }
  87.             else {
  88.                 echo '<h2 style="color:red">Invalid Feed.</h2>';
  89.             }
Advertisement
Add Comment
Please, Sign In to add comment