¿Eres nuevo? ¡Lee el FAQ y ponte al día!
1k visitas

Parse XML to array in PHP

This is the best function I have found to parse XML and save it as an associative array. I found it in the official PHP documentation: PHP: xml_parse

PHP
  1. <?php
  2. function xml2array($url, $get_attributes = 1, $priority = 'tag')
  3. {
  4.     $contents = "";
  5.     if (!function_exists('xml_parser_create'))
  6.     {
  7.         return array ();
  8.     }
  9.     $parser = xml_parser_create('');
  10.     if (!($fp = @ fopen($url, 'rb')))
  11.     {
  12.         return array ();
  13.     }
  14.     while (!feof($fp))
  15.     {
  16.         $contents .= fread($fp, 8192);
  17.     }
  18.     fclose($fp);
  19.     xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
  20.     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  21.     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  22.     xml_parse_into_struct($parser, trim($contents), $xml_values);
  23.     xml_parser_free($parser);
  24.     if (!$xml_values)
  25.         return; //Hmm...
  26.     $xml_array = array ();
  27.     $parents = array ();
  28.     $opened_tags = array ();
  29.     $arr = array ();
  30.     $current = & $xml_array;
  31.     $repeated_tag_index = array ();
  32.     foreach ($xml_values as $data)
  33.     {
  34.         unset ($attributes, $value);
  35.         extract($data);
  36.         $result = array ();
  37.         $attributes_data = array ();
  38.         if (isset ($value))
  39.         {
  40.             if ($priority == 'tag')
  41.                 $result = $value;
  42.             else
  43.                 $result['value'] = $value;
  44.         }
  45.         if (isset ($attributes) and $get_attributes)
  46.         {
  47.             foreach ($attributes as $attr => $val)
  48.             {
  49.                 if ($priority == 'tag')
  50.                     $attributes_data[$attr] = $val;
  51.                 else
  52.                     $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  53.             }
  54.         }
  55.         if ($type == "open")
  56.         {
  57.             $parent[$level -1] = & $current;
  58.             if (!is_array($current) or (!in_array($tag, array_keys($current))))
  59.             {
  60.                 $current[$tag] = $result;
  61.                 if ($attributes_data)
  62.                     $current[$tag . '_attr'] = $attributes_data;
  63.                 $repeated_tag_index[$tag . '_' . $level] = 1;
  64.                 $current = & $current[$tag];
  65.             }
  66.             else
  67.             {
  68.                 if (isset ($current[$tag][0]))
  69.                 {
  70.                     $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  71.                     $repeated_tag_index[$tag . '_' . $level]++;
  72.                 }
  73.                 else
  74.                 {
  75.                     $current[$tag] = array (
  76.                         $current[$tag],
  77.                         $result
  78.                     );
  79.                     $repeated_tag_index[$tag . '_' . $level] = 2;
  80.                     if (isset ($current[$tag . '_attr']))
  81.                     {
  82.                         $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  83.                         unset ($current[$tag . '_attr']);
  84.                     }
  85.                 }
  86.                 $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
  87.                 $current = & $current[$tag][$last_item_index];
  88.             }
  89.         }
  90.         elseif ($type == "complete")
  91.         {
  92.             if (!isset ($current[$tag]))
  93.             {
  94.                 $current[$tag] = $result;
  95.                 $repeated_tag_index[$tag . '_' . $level] = 1;
  96.                 if ($priority == 'tag' and $attributes_data)
  97.                     $current[$tag . '_attr'] = $attributes_data;
  98.             }
  99.             else
  100.             {
  101.                 if (isset ($current[$tag][0]) and is_array($current[$tag]))
  102.                 {
  103.                     $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  104.                     if ($priority == 'tag' and $get_attributes and $attributes_data)
  105.                     {
  106.                         $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  107.                     }
  108.                     $repeated_tag_index[$tag . '_' . $level]++;
  109.                 }
  110.                 else
  111.                 {
  112.                     $current[$tag] = array (
  113.                         $current[$tag],
  114.                         $result
  115.                     );
  116.                     $repeated_tag_index[$tag . '_' . $level] = 1;
  117.                     if ($priority == 'tag' and $get_attributes)
  118.                     {
  119.                         if (isset ($current[$tag . '_attr']))
  120.                         {
  121.                             $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  122.                             unset ($current[$tag . '_attr']);
  123.                         }
  124.                         if ($attributes_data)
  125.                         {
  126.                             $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  127.                         }
  128.                     }
  129.                     $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
  130.                 }
  131.             }
  132.         }
  133.         elseif ($type == 'close')
  134.         {
  135.             $current = & $parent[$level -1];
  136.         }
  137.     }
  138.     return ($xml_array);
  139. }
  140. ?>
  141.  

­

Etiquetas: PHP xml parse

Insertar: