XmlStreamReader
3 weeks ago
partner-discount-sdk
3 weeks ago
api.php
3 weeks ago
arraytoxml.php
3 weeks ago
chunk.php
3 weeks ago
config.php
2 years ago
download.php
3 weeks ago
error.php
3 weeks ago
handler.php
3 weeks ago
helper.php
3 weeks ago
input.php
3 weeks ago
nested.php
3 weeks ago
rapidaddon.php
3 weeks ago
render.php
3 weeks ago
session.php
10 months ago
upload.php
3 weeks ago
zip.php
10 years ago
arraytoxml.php
65 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 | |
| 17 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 18 | $data = apply_filters('wp_all_import_json_to_xml', $data); |
| 19 | |
| 20 | if ($xml == null) |
| 21 | { |
| 22 | $xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><'.$rootNodeName .'/>'); |
| 23 | } |
| 24 | |
| 25 | if ( !empty($data)){ |
| 26 | // loop through the data passed in. |
| 27 | foreach($data as $key => $value) |
| 28 | { |
| 29 | // no numeric keys in our xml please! |
| 30 | if (!$key or is_numeric($key)) |
| 31 | { |
| 32 | // make string key... |
| 33 | $key = "item_" . $lvl; |
| 34 | |
| 35 | } |
| 36 | |
| 37 | // replace anything not alpha numeric |
| 38 | // preg_replace('/^[0-9]+/i', '', preg_replace('/[^a-z0-9_]/i', '', $key)) |
| 39 | $key = preg_replace('/[^a-z0-9_]/i', '', $key); |
| 40 | |
| 41 | if ($key && is_numeric($key[0])) $key = 'v' . $key; |
| 42 | |
| 43 | // if there is another array found recrusively call this function |
| 44 | if (is_array($value) or is_object($value)) |
| 45 | { |
| 46 | $node = $xml->addChild($key); |
| 47 | // recrusive call. |
| 48 | PMXI_ArrayToXML::toXml($value, $rootNodeName, $node, $lvl + 1); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | // add single node. |
| 53 | $value = htmlspecialchars(preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $value)); |
| 54 | $xml->addChild($key, $value); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | } |
| 59 | } |
| 60 | // pass back as string. or simple xml object if you want! |
| 61 | return $xml->asXML(); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | } |