| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?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();
- }
- }
|