Jelajahi Sumber

added XML with readXmlFileToArray

Piotr Labudda 9 tahun lalu
induk
melakukan
8968dfdb83
1 mengubah file dengan 69 tambahan dan 0 penghapusan
  1. 69 0
      SE/se-lib/XML.php

+ 69 - 0
SE/se-lib/XML.php

@@ -0,0 +1,69 @@
+<?php
+
+Lib::loadClass('Core_XmlWriter');
+// $zxo = new XMLDiff\Memory;// @require xmldiff pecl package installed
+
+class XML {
+
+  public static function xmlToArray($xml) {
+
+  }
+
+  public static function readXmlFileToArray($filePath) {
+    $z = new XMLReader();
+    if (!$z) throw new HttpException("Class XMLReader not installed", 500);
+    if (!$z->open($filePath)) throw new Exception("Failed to open ant schema file for '{$item['namespace']}'", 500);
+    return self::xmlReadToArray($z);
+  }
+  public static function xmlReadToArray($z) {
+		while ($z->read()) {
+			if (XMLReader::ELEMENT !== $z->nodeType) continue;
+			return self::xmlReadRecurse($z);
+		}
+	}
+	public static function xmlReadRecurse($z) {
+		$node = [ $z->name, [], [] ];// name, attrs, childrens
+		$depth = $z->depth;
+		$isEmpty = $z->isEmptyElement;
+		if ($z->hasAttributes) {
+			while ($z->moveToNextAttribute()) {
+				$node[1][$z->name] = $z->value;
+			}
+		}
+		if ($isEmpty) {
+			$node[2] = null;
+			return $node;
+		}
+		// switch ($z->name) {
+		// 	case 'xsd:complexType': $node[1]['name'] = $z->getAttribute('name'); break;
+		// 	case 'xsd:simpleType': $node[1]['name'] = $z->getAttribute('name'); break;
+		// 	case 'xsd:restriction': $node[1]['base'] = $z->getAttribute('base'); break;
+		// 	case 'xsd:enumeration': $node[1]['value'] = $z->getAttribute('value'); break;
+		// 	case 'xsd:element': $node[1] = [
+		// 		'type' => $z->getAttribute('type')
+		// 	]; break;
+		// 	case 'xsd:complexContent': break;
+		// 	case 'xsd:sequence': break;
+		// 	case 'xsd:attribute': $node[1]['base'] = $z->getAttribute('base'); break;
+		// 	case 'xsd:extension': $node[1]['base'] = $z->getAttribute('base'); break;
+		// 	default: UI::alert('warning', "TODO: read attributes from: d({$z->depth}) name($z->name)");
+		// }
+		while ($z->read() && $z->depth > $depth) {
+			if (XMLReader::ELEMENT !== $z->nodeType) continue;
+			// if ($z->depth == $depth + 2)
+			$node[2][] = self::xmlReadRecurse($z);
+			// else $node[2][] = "d({$z->depth}) name($z->name)";
+		}
+		return $node;
+	}
+
+  public static function printXmlFromArray($xml) {
+    $xmlWriter = new Core_XmlWriter();
+    $xmlWriter->openUri('php://output');
+    $xmlWriter->setIndent(true);
+    $xmlWriter->startDocument('1.0','UTF-8');
+    $xmlWriter->h($xml[0], $xml[1], $xml[2]);
+    $xmlWriter->endDocument();
+  }
+
+}