Files
DomFramework/outputxml.php
2016-02-21 19:21:29 +00:00

52 lines
1.5 KiB
PHP

<?php
/** DomFramework
@package domframework
@author Dominique Fournier <dominique@fournier38.fr> */
require_once ("output.php");
/** Display in XML the data provided */
class outputxml extends output
{
/** Display in XML the data provided
@param mixed $data The data to be displayed */
public function out ($data)
{
@header("Cache-Control: no-store, no-cache, must-revalidate");
@header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
@header('Last-Modified: '.gmdate ('D, d M Y H:i:s').' GMT');
@header('Cache-Control: post-check=0, pre-check=0', false);
@header('Pragma: no-cache');
@header ("Content-Type: text/xml");
$xml = new SimpleXMLElement ("<?xml version=\"1.0\"?><root></root>");
// function call to convert array to xml
if (!is_array ($data))
$data = array ($data);
$this->array_to_xml ($data, $xml);
//saving generated xml file
print $xml->asXML();
if (!defined ("PHPUNIT"))
exit;
}
/** function defination to convert array to xml
@param mixed $data The data to be converted
@param string $xml The actual state of the XML file */
private function array_to_xml ($data, &$xml)
{
foreach($data as $key => $value)
{
if(is_array($value))
{
$key = is_numeric($key) ? "item$key" : $key;
$subnode = $xml->addChild("$key");
$this->array_to_xml($value, $subnode);
}
else
{
$key = is_numeric($key) ? "item$key" : $key;
$xml->addChild("$key","$value");
}
}
}
}