Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/08/2010, 17:31
Avatar de shanshito
shanshito
 
Fecha de Ingreso: noviembre-2008
Mensajes: 40
Antigüedad: 15 años, 5 meses
Puntos: 0
Exclamación Parsear HTML desde un PHP

Hola necesito parsear este html y obtener los datos, y estos datos crear un xml, pero me cuesta llegar a obtenerlos todos y acomodarlos!!!
Estoy usando simplehtmldom y lo bajan de aquí ---> http://simplehtmldom.sourceforge.net/ ;

este es mi HTML el cual necesito convertir en XML

Código HTML:
Ver original
  1. <tr>
  2.         <td valign="top" width="200"><div class="titulos_futuro_fisico">FUTURO</div>
  3.         <div class="div_columnas">
  4.           <div align="center"></div>
  5.           <table border="0" cellpadding="0" cellspacing="0" width="190">
  6.             <tbody><tr>
  7.               <td class="tiutlos_col_cuadro_merc_granos">MERCADO<br></td>
  8.               <td class="tiutlos_col_cuadro_merc_granos" width="50"><div align="center">POS<br>
  9.  
  10.               </div></td>
  11.               <td class="tiutlos_col_cuadro_merc_granos" width="45"><div align="center">U$S<br>
  12.               </div></td>
  13.               <td class="tiutlos_col_cuadro_merc_granos" width="50"><div align="right">VAR U$S<br>
  14.               </div></td>
  15.             </tr>
  16.            
  17.                 <tr>
  18.               <td>CBOT</td>
  19.  
  20.               <td width="50"><div align="center">NOV/10</div></td>
  21.               <td width="45"><div align="center">379,01</div></td>
  22.               <td width="50"><div align="right">
  23.                   <table border="0" cellpadding="0" cellspacing="0" width="100%">
  24.                     <tbody><tr>
  25.                       <td><div align="right"><img src="images/iconos/menos.gif" height="7" width="7"></div></td>
  26.                       <td width="33"><div align="right">-4,23</div></td>
  27.  
  28.                     </tr>
  29.                   </tbody></table>
  30.               </div></td>
  31.             </tr>
  32.                
  33.                 <tr>
  34.               <td>CBOT</td>
  35.               <td width="50"><div align="center">MAY/11</div></td>
  36.               <td width="45"><div align="center">381,03</div></td>
  37.  
  38.               <td width="50"><div align="right">
  39.                   <table border="0" cellpadding="0" cellspacing="0" width="100%">
  40.                     <tbody><tr>
  41.                       <td><div align="right"><img src="images/iconos/menos.gif" height="7" width="7"></div></td>
  42.                       <td width="33"><div align="right">-4,41</div></td>
  43.                     </tr>
  44.                   </tbody></table>
  45.               </div></td>
  46.  
  47.             </tr>
  48.                
  49.                 <tr>
  50.               <td>MATBA         </td>
  51.               <td width="50"><div align="center">NOV/10</div></td>
  52.               <td width="45"><div align="center">272,80</div></td>
  53.               <td width="50"><div align="right">
  54.                   <table border="0" cellpadding="0" cellspacing="0" width="100%">
  55.  
  56.                     <tbody><tr>
  57.                       <td><div align="right"><img src="images/iconos/mas.gif" height="7" width="7"></div></td>
  58.                       <td width="33"><div align="right">1,30</div></td>
  59.                     </tr>
  60.                   </tbody></table>
  61.               </div></td>
  62.             </tr>
  63.                
  64.                 <tr>
  65.  
  66.               <td>MATBA         </td>
  67.               <td width="50"><div align="center">MAY/11</div></td>
  68.               <td width="45"><div align="center">249,90</div></td>
  69.               <td width="50"><div align="right">
  70.                   <table border="0" cellpadding="0" cellspacing="0" width="100%">
  71.                     <tbody><tr>
  72.                       <td><div align="right"><img src="images/iconos/mas.gif" height="7" width="7"></div></td>
  73.  
  74.                       <td width="33"><div align="right">1,60</div></td>
  75.                     </tr>
  76.                   </tbody></table>
  77.               </div></td>
  78.             </tr>
  79.                
  80.           </tbody></table>
  81.         </div></td>
  82.     <td width="10">&nbsp;</td>
  83.  
  84.    
  85.     <td valign="top" width="200"><div class="titulos_futuro_fisico">FISICO</div>
  86.         <div class="div_columnas">
  87.           <table border="0" cellpadding="0" cellspacing="0" width="190">
  88.             <tbody><tr>
  89.               <td class="tiutlos_col_cuadro_merc_granos">MERCADO<br></td>
  90.               <td class="tiutlos_col_cuadro_merc_granos" width="55"><div align="right">PRECIO<br>
  91.               </div></td>
  92.  
  93.             </tr>
  94.             <tr align="center"><td colspan="3" height="100%" valign="middle">Sin Cotización</td></tr>
  95.           </tbody></table>
  96.         </div></td>
  97.     </tr>



aca les dejo el codigo de mi php.

Código PHP:
<?php

//http://simplehtmldom.sourceforge.net/
require("simplehtmldom/simple_html_dom.php");

$url="http://www.fyo.com/includes/cgGetMercadoGranos.asp?grano=1";
$html=file_get_contents($url);

 
/*** a new dom object ***/
    
$dom = new domDocument;

    
/*** load the html into the object ***/
    
$dom->loadHTML($html);

    
/*** discard white space ***/
    
$dom->preserveWhiteSpace false;

    
/*** the table by its tag name ***/
    
$tables $dom->getElementsByTagName('table');

    
/*** get all rows from the table ***/
    
$rows $tables->item(0)->getElementsByTagName('tr');

    
/*** loop over the table rows ***/
    
foreach ($rows as $row)
    {
        
/*** get each column by tag name ***/
        
$cols $row->getElementsByTagName('td');
        
/*** echo the values ***/
        
        
echo '<tr>'.$cols->item(0)->nodeValue.'</tr><br/>';

    }
?>

Aca simplemente lo que hago es porbar que puedo obtner los datos, pero me esta costando darme cuenta como ordenar los datos para que pueda grabarlos o guardarlos como yo quiera. asi me salen todos desarodenados .

Espero que alguien me pueda ayudar! Gracias por la sabiduría y la ayuda que siempre nos brindan a los desarrolladores de INTERNEEEEERRRRDD!!

abrazo!