Ver Mensaje Individual
  #1 (permalink)  
Antiguo 03/02/2002, 10:49
Avatar de Ferdy
Ferdy
Colaborador
 
Fecha de Ingreso: junio-2001
Ubicación: España
Mensajes: 1.430
Antigüedad: 23 años, 10 meses
Puntos: 0
Como empezar con PHP y XML

Bueno, pues voy a dar cuatro pautas cortas para empezar con PHP y XML. Lo primero es recompilar PHP con soporte para XML:<pre>[bash]# ./configure --enable-xml \
--enable-ftp \
--with-mysql \
--with-pgsql=shared \
--with-gd=shared \
--witch-apxs=/usr/local/apache/bin/apxs
[bash]# make &amp;&amp; make install </pre> Y ahora vamos con un ejemplillo sencillísimo:
prueba.xml <pre>
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;frases&gt;
&lt;frase&gt;Hola a todos. &lt;negrita&gt;Ferdy&lt;/negrita&gt;&lt;/frase&gt;
&lt;frase&gt;Adios a todos.&lt;cursiva&gt;Ferdy&lt;/cursiva&gt;&lt;/frase&gt;
&lt;/frases&gt; </pre>
prueba.php <pre>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Prueba con XML&lt;/title&gt;
&lt;/head&gt;
&lt;body bgcolor=&quot;#FFFFFF&quot;&gt;
&lt;?php

$fichero = &quot;prueba.xml&quot;;

$inicio = array(
&quot;FRASES&quot; =&gt; &quot;&lt;ul&gt;&quot;,
&quot;FRASE&quot; =&gt; &quot;&lt;li&gt;&quot;,
&quot;NEGRITA&quot; =&gt; &quot;&lt;b&gt;&quot;,
&quot;CURSIVA&quot; =&gt; &quot;&lt;i&gt;&quot;
);
$fin = array(
&quot;FRASES&quot; =&gt; &quot;&lt;/ul&gt;&quot;,
&quot;FRASE&quot; =&gt; &quot;&lt;/li&gt;&quot;,
&quot;NEGRITA&quot; =&gt; &quot;&lt;/b&gt;&quot;,
&quot;CURSIVA&quot; =&gt; &quot;&lt;/i&gt;&quot;
);
function inicio ( $parser , $nombre , $attrs = &quot;&quot; )
{
if (isset($GLOBALS['inicio'][$nombre]))
{
echo $GLOBALS['inicio'][$nombre];
}
return;
}

function fin ( $parser , $name , $attrs = &quot;&quot; )
{
if (isset($GLOBALS['fin'][$nombre]))
{
echo $GLOBALS['fin'][$nombre];
}
return;
}

function datos ( $parser , $dat )
{
echo $dat;
return;
}

$xml = xml_parser_create();
xml_set_element_handler($xml,&quot;inicio&quot;,&q uot;fin&quot;);
xml_set_character_data_handler($xml,&quot;datos&qu ot;);

$ret = xml_parse_from_file($xml,$fichero);
if (!$ret)
{
die(sptrinf(&quot;XML Error: %s en la línea %d de %s&quot;,
xml_error_string(xml_get_error_code($xml)),
xml_get_current_line_number($xml),
$fichero));
}

xml_parser_free($xml);

function xml_parse_from_file($parser,$fichero)
{
if (!($fp = fopen($fichero,&quot;r&quot;)))
{
die(&quot;No se pudo encontrar el fichero de datos XML: $fichero&quot;);
return false;
}

while ($dat = fread($fp,4096))
{
if (!xml_parse($parser,$dat,feof($fp)))
{
fclose($fp);
return false;
}
}

fclose($fp);
return true;
}

?&gt; </pre> Bueno, espero que os sirvan ;)

Salu2.Feliz Codding

- Fernando Pereda ( Ferdy )
<center><a href="http://www.ferdyx.org/firmas.php"><img src="http://www.ferdyx.org/reg_tira.jpg" border="0"></a></center>