Przeglądaj źródła

Added arrayToXML function do V class

Mariusz Muszyński 8 lat temu
rodzic
commit
d2372180be
1 zmienionych plików z 27 dodań i 0 usunięć
  1. 27 0
      SE/se-lib/V.php

+ 27 - 0
SE/se-lib/V.php

@@ -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();
+	}
+		
 }