Your IP : 216.73.216.86


Current Path : /var/www/homesaver/www/bitrix/modules/epages.pickpoint/
Upload File :
Current File : /var/www/homesaver/www/bitrix/modules/epages.pickpoint/array.to.xml.php

<?php

class Array2XML
{
    private $writer;
    private $version = '1.0';
    private $encoding = 'UTF-8';
    private $rootName = 'root';

    public function __construct()
    {
        $this->writer = new XMLWriter();
    }

    public function convert($data)
    {
        $this->writer->openMemory();
        $this->writer->startDocument($this->version, $this->encoding);
        $this->writer->startElement($this->rootName);
        if (is_array($data)) {
            $this->getXML($data);
        }
        $this->writer->endElement();

        return $this->writer->outputMemory();
    }

    public function setVersion($version)
    {
        $this->version = $version;
    }

    public function setEncoding($encoding)
    {
        $this->encoding = $encoding;
    }

    public function setRootName($rootName)
    {
        $this->rootName = $rootName;
    }

    private function getXML($data)
    {
        foreach ($data as $key => $val) {
            if (is_numeric($key)) {
                $key = 'key'.$key;
            }
            if (is_array($val)) {
                $this->writer->startElement($key);
                $this->getXML($val);
                $this->writer->endElement();
            } else {
                $this->writer->writeElement($key, $val);
            }
        }
    }
}