1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
<?php
/*
* Converts associative arrays to XML and JSON using PHPs XMLWriter
*
* @author Open Dynamics <info@o-dyn.de>
* @name toXml
* @private $writer PHP XML Writer object
* @version 0.4.8
* @package Collabtive
* @link http://www.o-dyn.de
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or later
*/
class toXml
{
private $writer;
function __construct()
{
//create XMLWriter Object
$this->writer = (object) new XMLWriter();
}
/**
* Convert array to complete XML document.
* Accepts an associative array.
* Also accepts a numeric and associative indexed array (as for example returned by mysql_fetch_array()),
* but strips the numeric index from those.
*
* @param array $inarr Array to convert
* @param string $rootname Name of the root XML element
* @return string Array as XML
*/
public function arrToXml(array $inarr, $rootname)
{
$writer = $this->writer;
//$writer->openURI('php://output');
$writer->openMemory();
//start a new document
$writer->startDocument("1.0","utf-8","yes");
$writer->setIndent(true);
//create the root element
$writer->startElement($rootname);
// $writer->writeAttribute("count",count($inarr));
//convert the array recursively
$this->convertit($inarr);
//end the root element
$writer->endElement();
//close the document
$writer->endDocument();
//return the XML as string
return $writer->outputMemory();
}
/**
* Convert array to JSON.
* Accepts an associative array.
* Also accepts a numeric and associative indexed array (as for example returned by mysql_fetch_array())
* This is a wrapper around PHPs json_encode()
*
* @param array $inarr Array to convert
* @return string Array as JSON
*/
public function arrToJSON(array $inarr)
{
return json_encode($inarr);
}
/**
* Private function that recursively converts an Array to XML.
* Used by arrToXml()
* Accepts an associative array.
* Also accepts a numeric and associative indexed array (as for example returned by mysql_fetch_array()),
* but strips the numeric index from those.
*
* @param array $inarr Array to convert
*
* @return void
*/
private function convertit(array $inarr)
{
$writer = $this->writer;
foreach($inarr as $key => $val)
{
//if value is an array again, recursively call this function
if(is_array($val))
{
//if key is numeric, convert it to a string
if(is_numeric($key))
{
$numkey = $key;
$key = "node";
$writer->startElement($key);
//$writer->writeAttribute("num",$numkey);
}
else
{
$writer->startElement($key);
}
//convert $val recursively
$this->convertit($val);
$writer->endElement();
}
else
{
if(!is_numeric($key))
{
$writer->writeElement($key,strip_tags($val));
}
}
}
}
}
?>
|