PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/array2xml.php

https://github.com/MilkZoft/zan
PHP | 65 lines | 65 code | 0 blank | 0 comment | 1 complexity | 88415b4878ed18c8449a5bbf25ab566a MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. class ZP_Array2XML
  6. {
  7. private $XML = NULL;
  8. private function build($array, $ID)
  9. {
  10. if (is_array($array)) {
  11. $keys = array_keys($array);
  12. for($i = 0; $i < sizeof($keys); $i++) {
  13. $tag = $keys[$i];
  14. if (is_numeric($tag)) {
  15. $tag = $ID;
  16. }
  17. if ($tag === "_id") {
  18. $tag = "id";
  19. }
  20. $this->XML .= ($tag === $ID) ? "<". strtolower($tag) .">" : "<". strtolower($tag) .">";
  21. $this->build($array[$keys[$i]], $ID);
  22. $this->XML .= ($tag === $ID) ? "</". strtolower($tag) .">" : "</". strtolower($tag) .">";
  23. }
  24. } elseif (!empty($array)) {
  25. if ($this->checkForHTML($array)) {
  26. $array = '<![CDATA['. $array .']]>';
  27. }
  28. $this->XML .= $array;
  29. } else {
  30. return false;
  31. }
  32. }
  33. private function checkForHTML($string)
  34. {
  35. if (strlen($string) !== strlen(strip_tags($string))) {
  36. return true;
  37. }
  38. return false;
  39. }
  40. public function printXML($array, $root = "data", $ID = "node")
  41. {
  42. $this->toXML($array, $root, $ID);
  43. header("Content-Type: text/xml");
  44. print $this->XML;
  45. }
  46. public function toXML($array, $root = "data", $ID = "node")
  47. {
  48. $this->XML .= '<?xml version="1.0" encoding="UTF-8"?>';
  49. $this->XML .= "<". strtolower($root) .">";
  50. $this->XML .= $this->build($array, $ID);
  51. $this->XML .= "</". strtolower($root) .">";
  52. return $this->XML;
  53. }
  54. }