|
|
|
Class to generate XML from databse table
1
This is a simple class for generating XML from MySql databse table.
Usage
include("Xml.class.php");//include class file
$mx=new Xml;//create new Xml object
$mx->dbtoXml("localhost","root","","dbname","tblname");//give hostname, user, password, database name and table name
echo $mx->showXml();//showXml returns Xml
Usage
include("Xml.class.php");//include class file
$mx=new Xml;//create new Xml object
$mx->dbtoXml("localhost","root","","dbname","tblname");//give hostname, user, password, database name and table name
echo $mx->showXml();//showXml returns Xml
<?php
class Xml{
var $xop;
private function KeyName(array $a, $pos)
{
$temp = array_slice($a, $pos, 1, true);
return key($temp);
}
public function dbtoXml($host,$user,$pass,$dbname,$tblname)
{
$connection = mysql_connect($host, $user, $pass) or die("Could not connect.");
$db = mysql_select_db($dbname, $connection);
$qry = "select * from " . $tblname;
$result = mysql_query($qry, $connection) or die("Could not complete database query");
$this->xop="<?xml version=\"1.0\" ?>\n";
$this->xop.="<root>\n";
$i=0;
$keyarray=array();
while($at=mysql_fetch_assoc($result))
{
$keyarray[]=$this->KeyName($at,$i);$i++;
}
array_pop($keyarray);
$result = mysql_query($qry, $connection) or die("Could not complete database query");
while($a=mysql_fetch_array($result))
{
$i=0;
$this->xop.="<content>\n";
for($j=0;$j<count($keyarray);$j++)
{
$this->xop.="<".$keyarray[$j].">".$a[$i]."</".$keyarray[$j].">"."\n";
$i++;
}
$this->xop.="</content>\n";
}
$this->xop.="</root>";
}
public function showXml()
{
return $this->xop;
}
}//end class
?>




There are currently no comments for this snippet.