PHPExcel
10 years ago
PHPExcel.php
10 years ago
api.php
10 years ago
arraytoxml.php
10 years ago
chunk.php
10 years ago
config.php
10 years ago
download.php
10 years ago
handler.php
10 years ago
helper.php
10 years ago
input.php
10 years ago
render.php
10 years ago
session.php
10 years ago
upload.php
10 years ago
arraytoxml.php
62 lines
| 1 | <?php |
| 2 | |
| 3 | class PMXI_ArrayToXML |
| 4 | { |
| 5 | /** |
| 6 | * The main function for converting to an XML document. |
| 7 | * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. |
| 8 | * |
| 9 | * @param array $data |
| 10 | * @param string $rootNodeName - what you want the root node to be - defaultsto data. |
| 11 | * @param SimpleXMLElement $xml - should only be used recursively |
| 12 | * @return string XML |
| 13 | */ |
| 14 | public static function toXml($data, $rootNodeName = 'data', $xml=null, $lvl = 0) |
| 15 | { |
| 16 | // turn off compatibility mode as simple xml throws a wobbly if you don't. |
| 17 | if (ini_get('zend.ze1_compatibility_mode') == 1) |
| 18 | { |
| 19 | ini_set ('zend.ze1_compatibility_mode', 0); |
| 20 | } |
| 21 | |
| 22 | if ($xml == null) |
| 23 | { |
| 24 | $xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><'.$rootNodeName .'/>'); |
| 25 | } |
| 26 | if ( !empty($data)){ |
| 27 | // loop through the data passed in. |
| 28 | foreach($data as $key => $value) |
| 29 | { |
| 30 | // no numeric keys in our xml please! |
| 31 | if (!$key or is_numeric($key)) |
| 32 | { |
| 33 | // make string key... |
| 34 | $key = "item_" . $lvl; |
| 35 | |
| 36 | } |
| 37 | |
| 38 | // replace anything not alpha numeric |
| 39 | $key = preg_replace('/[^a-z0-9_]/i', '', $key); |
| 40 | |
| 41 | // if there is another array found recrusively call this function |
| 42 | if (is_array($value) or is_object($value)) |
| 43 | { |
| 44 | $node = $xml->addChild($key); |
| 45 | // recrusive call. |
| 46 | PMXI_ArrayToXML::toXml($value, $rootNodeName, $node, $lvl + 1); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | // add single node. |
| 51 | $value = htmlspecialchars($value); |
| 52 | $xml->addChild($key,$value); |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | } |
| 57 | // pass back as string. or simple xml object if you want! |
| 58 | return $xml->asXML(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | } |