Thursday, May 21, 2009

Creating xml files

creating xml file as output of your php file execution can be done by using php parser
-here in terms of object the xml file can be created.…..

The xml file will be created on bases of the class object u created……each tag will be having separate object….
creating xml file consist of following steps

Step 1)create object class of the table from which your going to access data.

The following sample shows how to create the class obj
/*
* This class insert or fetches values from/to XML file.
*/

class Class_name //giv table name as class name for better working
{
private $id;
private $name;


public function getId() //get function for accessing the data
{
return $this->id;
}

public function setId($param)//set function to insert data into the table
{
$this->id = $param;
}
.
.
.etc…

}
?>
Step2)in this step just copy this parser code and paste in new php file and name it as “parser.php “ this following code consist of create and getAll function. Where create function creates the xml file by using obj of the class which u created by using table
And the getAll function is used to get the data from the xml file which u created.


include_once "Class_name.php"; // the object class which u created
class Parser
{
public static function getAll($file) //to get data from the xml object
{
if(file_exists($file.'.xml'))
{
$objxml = new Parser;
$xmlobj = simplexml_load_file($file.".xml");
$class_name = ucfirst($xmlobj->class_name);
$obj = new $class_name();
foreach ($objxml->getClassMethods($class_name) as $method)
{
$length = strlen($method);
$field = substr($method, 3, $length);
$type = substr($method, 0, 3);
if($type == 'set')
{
$field = strtolower($field);
$obj->$method($xmlobj->$field);
}
}
$object[] = $obj;
return $object;
}
else
{
return false;
}
}

public static function create($obj, $file) //to create the xml file
{
$objxml = new Parser;
$class_name = $objxml->getClass($obj);
foreach ($objxml->getClassMethods($class_name) as $method)
{
$length=strlen($method);
$field=substr($method, 3, $length);
$type=substr($method, 0, 3);
if($type=='get')
{
$field = strtolower($field);
$val[$field] = $obj->$method();
}
}
$objxml->xml_file_create($val, $file, $class_name);
}

public function create_xml_nodes($obj, $array)
{
foreach($array as $key => $value)
{
if(is_array($value))
{
$parentobj = $obj->addChild($key);
$this->create_xml_nodes($parentobj, $value);
}
else
{
$obj->addChild($key, $value);
}
}
}
private function xml_file_create($data, $file, $class_name)
{
$fp = fopen($file.".xml","w");
$xmltext = "\n";
$xmlobj = simplexml_load_string($xmltext);
$xmlobj->addChild("class_name", $class_name);
foreach($data as $key => $value)
{
if(is_array($value))
{
$parentobj = $xmlobj->addChild($key);
$length = strlen($key);
$key = substr($key, 0, $length-1);
foreach($value as $val)
{
$childobj = $parentobj->addChild($key);
$this->create_xml_nodes($childobj, $val);
}
}
else
{
$xmlobj->addChild($key, $value);
}
}
//Writing on to file.
fwrite($fp, $xmlobj->asXML());
fclose($fp);
}

private function getClassMethods($classname) //to access the class obj
{
$methods = array();
if (!class_exists($classname))
{
throw new Exception ("Class $classname does not exists.");
}
$reflector = new ReflectionClass($classname);
$reflected_methods = $reflector->getMethods();

foreach ($reflected_methods as $reflected)
{
$methods[] = $reflected->getName();
}

return $methods;
}

private function getClass($instance)
{
$reflector = new ReflectionObject($instance);

if (!$reflector->isInstance($instance))
{
return false;
}

return $reflector->getName();
}
}
?>

Step3)In next step create the example php file as “test.php “which consist of calling create and getAll function…with example…..


require_once 'Parser.php';
$parser = new Parser;

$tpl = new Class_name();
$tpl->setId("100");//u can give any id or pass any value wich
$tpl->setName("TESTNAME");

$parser->create($tpl,"xmlfile"); //here “xmlfile” is the name in which xml file will be created


// the following function is optional if u want to display the content of the xml file then the following code will work based on the obj class and the xml tags will be created on obj….
$data = $parser->getAll("tpl_sample_4");//takes file name as input.
echo "Id :: ".$data[0]->getId().'
';
echo "Name :: ".$data[0]->getName().'
';
//print_r($array);


?>

The out put of this test.php is

Id :: 100
Name :: TESTNAME

No comments: