|
|
@@ -542,4 +542,31 @@ EOF';
|
|
|
return $items;
|
|
|
}
|
|
|
|
|
|
+ public static function arrayToXML($array, $formatOutput = false, $root = "root") {
|
|
|
+
|
|
|
+ function arrayToXML_rec($data, $dom, $node, $parent = null) {
|
|
|
+ $child = $dom->createElement($node);
|
|
|
+ if (!$parent) $parent = $dom;
|
|
|
+ if (is_array($data)) {
|
|
|
+ foreach ($data as $key => $value) {
|
|
|
+ if (is_numeric($key)) arrayToXML_rec($value, $dom, $node, $parent);
|
|
|
+ else arrayToXML_rec($value, $dom, $key, $child);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if ($data) {
|
|
|
+ if ($data == htmlspecialchars($data)) $child->nodeValue = $data;
|
|
|
+ else $child->appendChild($dom->createCDATASection($data));
|
|
|
+ } else $parent->appendChild($child);
|
|
|
+ }
|
|
|
+ if ($child->hasChildNodes()) $parent->appendChild($child);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_array($array)) throw new Exception("First argument need to be an array");
|
|
|
+ $dom = new DOMDocument('1.0', 'UTF-8');
|
|
|
+ $dom->preserveWhiteSpace = false;
|
|
|
+ $dom->formatOutput = $formatOutput;
|
|
|
+ arrayToXML_rec($array, $dom);
|
|
|
+ return $dom->saveXML();
|
|
|
+ }
|
|
|
+
|
|
|
}
|