Table Data to XML File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

Table Data to XML File

Post by mccommunity »

I am trying to take a tables data and output and XML file from it. I am not really sure how though. Does anyone have a script to do this or can point me in the right direction so I can see samles of how this is done? Thanks.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You could, for example, create an .xml template and embed some echo calls.

Declare all the vars then simply include the template to print to client.

Or wrap it up in a buffer to write to file.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

I am not sure if you want to retrieve DB information and have a xml file created.. but if its like that.. this is what i use.

Code: Select all

// db connection bla... bla..

$num = mysql_num_rows($result); 


$file= fopen("resulta.xml", "w"); 
$_xml ="<?xml version="1.0" encoding="UTF-8"?>\r\n"; 
$_xml .="<cardat>\r\n"; 
$_xml .="<envctrol>\r\n";
if(mysql_num_rows($result) > 0){ 
while($rows = mysql_fetch_array($result)) 
   {
	  //$_xml .="\t<dato="" . $rows["dato"] . "">\r\n";$_xml .="\t</dato>\r\n"; 
      $_xml .="\t<tipo-operador>="08">\r\n";$_xml .="\t</tipo-operador>\r\n"; 
      $_xml .="\t<codigo-operador> ="5519">\r\n";$_xml .="\t</codigo-operador>\r\n"; 
	  $_xml .="\t<nume-regist-cabe>="1">\r\n";$_xml .="\t</nume-regist-cabe>\r\n"; 
	  $_xml .="\t<nume-regist-cabe-dav>="1">\r\n";$_xml .="\t</nume-regist-cabe-dav>\r\n"; 
      $_xml .="\t<nume-regist-doc-transp>="1">\r\n";$_xml .="\t</nume-regist-doc-transp>\r\n"; 
      $_xml .="\t<t-fob-doc-transp>="20">\r\n";$_xml .="\t</t-fob-doc-transp>\r\n"; 
      $_xml .="\t<t-peso-bruto-manifdo>="20">\r\n";$_xml .="\t</t-peso-bruto-manifdo>\r\n"; 
      $_xml .="\t<clave-electronica>="5519">\r\n";$_xml .="\t</clave-electronica>\r\n"; 

// and 250 lines more 

 }
	  }
      else 
         { 
         $_xml .="\t<Query_Empty>="No Resulta">\r\n"; 
         $_xml .="\t</Query_Empty>\r\n"; 
         } 
         $_xml .="</envctrol>";
         $_xml .="</cardat>"; 
         fwrite($file, $_xml); 
         fclose($file); 
         echo "XML formato list.  <a href="resulta.xml">Por Aqui el formato XML.</a>"; 

?>

Code: Select all

// $_xml .="\t<xml tag>="" . $rows["some field"] . "">\r\n";$_xml .="\t</xml tag>\r\n";
Hope that helps.
Post Reply