array2xml.php 845 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace app\common;
  3. class array2xml {
  4. private $version = '1.0';
  5. private $encoding = 'UTF-8';
  6. private $xml = null;
  7. function __construct() {
  8. $this->xml = new \XMLWriter();
  9. }
  10. public function toXml($data, $eIsArray=FALSE) {
  11. if(!$eIsArray) {
  12. $this->xml->openMemory();
  13. $this->xml->startDocument($this->version, $this->encoding);
  14. }
  15. foreach($data as $key => $value){
  16. if(is_array($value)){
  17. $this->xml->startElement($key);
  18. $this->toXml($value, TRUE);
  19. $this->xml->endElement();
  20. continue;
  21. }
  22. $this->xml->writeElement($key, $value);
  23. }
  24. if(!$eIsArray) {
  25. $this->xml->endElement();
  26. return $this->xml->outputMemory(true);
  27. }
  28. }
  29. }