XML.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. Lib::loadClass('Core_XmlWriter');
  3. // $zxo = new XMLDiff\Memory;// @require xmldiff pecl package installed
  4. class XML {
  5. public static function xmlToArray($xml) {
  6. }
  7. public static function readXmlFileToArray($filePath) {
  8. $z = new XMLReader();
  9. if (!$z) throw new HttpException("Class XMLReader not installed", 500);
  10. if (!$z->open($filePath)) throw new Exception("Failed to open ant schema file for '{$item['namespace']}'", 500);
  11. return self::xmlReadToArray($z);
  12. }
  13. public static function xmlReadToArray($z) {
  14. while ($z->read()) {
  15. if (XMLReader::ELEMENT !== $z->nodeType) continue;
  16. return self::xmlReadRecurse($z);
  17. }
  18. }
  19. public static function xmlReadRecurse($z) {
  20. $node = [ $z->name, [], [] ];// name, attrs, childrens
  21. $depth = $z->depth;
  22. $isEmpty = $z->isEmptyElement;
  23. if ($z->hasAttributes) {
  24. while ($z->moveToNextAttribute()) {
  25. $node[1][$z->name] = $z->value;
  26. }
  27. }
  28. if ($isEmpty) {
  29. $node[2] = null;
  30. return $node;
  31. }
  32. // switch ($z->name) {
  33. // case 'xsd:complexType': $node[1]['name'] = $z->getAttribute('name'); break;
  34. // case 'xsd:simpleType': $node[1]['name'] = $z->getAttribute('name'); break;
  35. // case 'xsd:restriction': $node[1]['base'] = $z->getAttribute('base'); break;
  36. // case 'xsd:enumeration': $node[1]['value'] = $z->getAttribute('value'); break;
  37. // case 'xsd:element': $node[1] = [
  38. // 'type' => $z->getAttribute('type')
  39. // ]; break;
  40. // case 'xsd:complexContent': break;
  41. // case 'xsd:sequence': break;
  42. // case 'xsd:attribute': $node[1]['base'] = $z->getAttribute('base'); break;
  43. // case 'xsd:extension': $node[1]['base'] = $z->getAttribute('base'); break;
  44. // default: UI::alert('warning', "TODO: read attributes from: d({$z->depth}) name($z->name)");
  45. // }
  46. while ($z->read() && $z->depth > $depth) {
  47. if (XMLReader::ELEMENT !== $z->nodeType) continue;
  48. // if ($z->depth == $depth + 2)
  49. $node[2][] = self::xmlReadRecurse($z);
  50. // else $node[2][] = "d({$z->depth}) name($z->name)";
  51. }
  52. return $node;
  53. }
  54. public static function printXmlFromArray($xml) {
  55. $xmlWriter = new Core_XmlWriter();
  56. $xmlWriter->openUri('php://output');
  57. $xmlWriter->setIndent(true);
  58. $xmlWriter->startDocument('1.0','UTF-8');
  59. $xmlWriter->h($xml[0], $xml[1], $xml[2]);
  60. $xmlWriter->endDocument();
  61. }
  62. }