Personally, I would do it differently, but thats just me... However one
modification I would suggest you make:

The Line:
   $xml.="<".$nombre_campo.">".$valor_campo."</".$nombre_campo.">\n";

would probably be better as this:

$xml.="<".$nombre_campo.">".htmlspecialchars($valor_campo)."</".$nombre_camp
o.">\n";

just incase there is an & or a " in the data, and some XML parsers will
throw an error saying that the XML file is not well formed.

-----Original Message-----
From: Octavio Herrera [mailto:[EMAIL PROTECTED]]
Sent: Sunday, 26 January 2003 9:36 AM
To: [EMAIL PROTECTED]
Subject: [PHP] converting a Recorset into an XML string


Hello everybody, I make a function that lets you convert a recordset
obtained via mysql_query() into an XML string:
maybe could be helpfull to yours

this is the function

it have 3 parameters:   $rs is the recordset you want to convert
                                    $padre is the name for the parent node
that will contain every record on the recordset
                                    $hijo is the name for the child node
that will contain a single record in the recordset



function rs2xml($rs,$padre,$hijo){
 $num_campos=mysql_num_fields($rs);
 $num_filas=mysql_num_rows($rs);

 if($padre!="-1")
  $xml="<$padre>\n";
 else
  $xml="";

 $cont1=0;
 while($cont1<$num_filas){
  $fila=mysql_fetch_array($rs);
  $xml.="<$hijo>\n";
  $cont=0;
  while($cont<$num_campos){
   $nombre_campo=mysql_field_name($rs,$cont);
   $valor_campo=$fila[$cont];
   $xml.="<".$nombre_campo.">".$valor_campo."</".$nombre_campo.">\n";
   $cont++;
  }
  $xml.="</$hijo>\n";
  $cont1++;
 }
 if($padre!="-1")
 $xml.="</$padre>\n";
 return $xml;
}

I hope this will be helpful for you

OCTAVIO HERRERA



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to