Ver Mensaje Individual
  #8 (permalink)  
Antiguo 07/09/2009, 14:51
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 11 meses
Puntos: 20
Respuesta: Problema simpleXML

He conseguido iterarlo a mi manera xDD

¿Me podéis dar vuestra opinión? ¿lo hariáis de otra forma? Al final lo que quiero conseguir es introducir estos datos en una bbdd mysql

Código php:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Analizar XML</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. </head>
  7. <body>
  8. <?php
  9.  
  10. class xml {
  11.  
  12. var $matriz = array();
  13. var $resultado;
  14. var $informacion;
  15.  
  16. function xml($contenido) {
  17.  
  18.         $this->resultado = xml_parser_create ();
  19.         xml_set_object($this->resultado,$this);
  20.         xml_set_element_handler($this->resultado, "abrir", "cerrar");
  21.        
  22.         xml_set_character_data_handler($this->resultado, "info");
  23.    
  24.         $this->informacion = xml_parse($this->resultado,$contenido);
  25.                      
  26.         xml_parser_free($this->resultado);
  27.        
  28.         return $this->matriz;
  29. }
  30. function abrir($parser, $nombre, $atributos) {
  31.     $etiqueta = array("nombre"=>$nombre,"atributos"=>$atributos);
  32.     array_push($this->matriz,$etiqueta);
  33. }
  34.  
  35. function info($parser, $etiqueta_info) {
  36.     if(trim($etiqueta_info)) {
  37.         if(isset($this->matriz[count($this->matriz)-1]['info'])) {
  38.            $this->matriz[count($this->matriz)-1]['info'] .= $etiqueta_info;
  39.         }
  40.         else {
  41.            $this->matriz[count($this->matriz)-1]['info'] = $etiqueta_info;
  42.         }
  43.     }
  44. }
  45.  
  46. function cerrar($parser, $nombre) {
  47.    $this->matriz[count($this->matriz)-2]['hijo'][] = $this->matriz[count($this->matriz)-1];
  48.     array_pop($this->matriz);
  49. }
  50. }
  51.  
  52. // XML origen
  53. $url = "http://partnerfeed.itsfogo.com/partnerfeed.aspx?partnerfeedID=1305&ZoneID=34296&partnerTargetLink=&partnerField=itsfogoTargetLink";
  54. $xml_contenido = file_get_contents($url,'r');
  55.  
  56. $xml = new xml($xml_contenido);
  57.  
  58. echo '<pre>';
  59. //print_r($xml->matriz);
  60. $arreglo = ($xml->matriz);
  61. echo '</pre>';
  62.  
  63.  
  64. // Cuento el numero de ligas
  65. $num_ligas = count($arreglo[0]['hijo']);
  66. echo "Numero de ligas: $num_ligas <br/>";
  67.  
  68. // Cuento el numero de partidos
  69. $num_partidos = count($arreglo[0]['hijo'][0]['hijo']);
  70. echo "Numero de partidos: $num_partidos <br/>";
  71.  
  72. // Cuento los mercados disponibles
  73. $num_mercados = count($arreglo[0]['hijo'][0]['hijo'][0]['hijo']);
  74. echo "Numero de mercados: $num_mercados <br/>";
  75.  
  76. // Cuento apuestas disponibles de ese mercado
  77. $num_apuestas = count($arreglo[0]['hijo'][0]['hijo'][0]['hijo'][0]['hijo']);
  78. echo "Numero de apuestas para el mercado: $num_apuestas <br/>";
  79.  
  80. for ($i = 0; $i < $num_ligas; $i++) {
  81.  
  82.     if ($arreglo[0]['hijo'][$i]['atributos']['NAME'] != ''){
  83.         // imprime el nombre de la competicion
  84.         echo "<br/>" . $arreglo[0]['hijo'][$i]['atributos']['NAME'] . "<br/>";
  85.        
  86.         $num_partidos = count($arreglo[0]['hijo'][$i]['hijo']);
  87.         $num_apuestas = count($arreglo[0]['hijo'][$i]['hijo'][0]['hijo'][0]['hijo']);
  88.         echo "Numero de partidos: $num_partidos <br/><br/>";
  89.         for ($j = 0; $j < $num_partidos; $j++) {
  90.             // imprime el nombre del partido
  91.             echo $arreglo[0]['hijo'][$i]['hijo'][$j]['atributos']['NAME'] . "<br/>";
  92.             // imprime la fecha del partido
  93.             //echo $arreglo[0]['hijo'][$i]['hijo'][$j]['atributos']['EVENTDATE'] . "<br/>";
  94.             // imprime el enlace del partido
  95.             //echo $arreglo[0]['hijo'][$i]['hijo'][$j]['atributos']['EVENTLINK'] . "<br/>";
  96.             $num_mercados = count($arreglo[0]['hijo'][0]['hijo'][0]['hijo']);
  97.             for ($k = 0; $k < $num_mercados; $k++) {
  98.                 //Nombre del mercado
  99.                 //echo $arreglo[0]['hijo'][$i]['hijo'][$j]['hijo'][$k]['atributos']['NAME'];
  100.                
  101.                 for ($l = 0; $l < $num_apuestas; $l++) {
  102.                     // imprime el nombre de la apuesta
  103.                     echo $arreglo[0]['hijo'][$i]['hijo'][$j]['hijo'][$k]['hijo'][$l]['atributos']['NAME'];
  104.                     // imprime la cuota de la apuesta
  105.                     echo $arreglo[0]['hijo'][$i]['hijo'][$j]['hijo'][$k]['hijo'][$l]['atributos']['ODD'];
  106.                     // imprime el enlace de la apuesta
  107.                     //echo $arreglo[0]['hijo'][$i]['hijo'][$j]['hijo'][$k]['hijo'][$l]['atributos']['PLACEBETLINK'];
  108.                 }
  109.             }
  110.         }
  111.        
  112.     }
  113. }
  114.  
  115.  
  116. ?>
  117.  
  118. </body>
  119. </html>

Última edición por neodani; 09/09/2009 a las 15:01