class XMLFile
{
public $fullArray = array ();
private $writer;
private $version = '1.0';
private $encoding = 'UTF-8';
private $rootName = 'root';
private $saveRoot = 'SettingArray.xml';
public function XmlToArray ($element, $arr = array())
{
if (is_string ($element))
{
$element = (strlen ($element) > 5 && substr ($element, -4) === '.xml')
? simplexml_load_file (__DIR__. "/". $element)
: simplexml_load_string ($element);
}
foreach ($element->children () as $base)
{
$sub = $base->getName ();
if (!$base->children ())
{
$arr[$sub] = trim ($base [0]);
}
else
{
$arr[$sub]= array();
$arr[$sub] = $this->XmlToArray ($base, $arr [$sub]);
}
}
return $arr;
}
public function convertXML ($data)
{
$this->writer = new XMLWriter();
$this->writer->openMemory();
$this->writer->setIndent ('on');
$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();
}
private function getXML ($data)
{
foreach ($data as $key => $val)
{
if (is_numeric($key))
{
$key = 'item'. $key;
}
if (is_array ($val))
{
$this->writer->startElement($key);
$this->getXML($val);
$this->writer->endElement();
}
else
{
$this->writer->writeElement($key, $val);
}
}
}
public function saveArray ($element = array ())
{
foreach ($element as $base)
{
$this->fullArray = array_merge_recursive ((array) $this->fullArray, (array) $this->XmlToArray ($base));
}
return $this->fullArray;
}
public function saveXML ($element)
{
$xml = file_put_contents ($this->saveRoot, $element);
return $xml;
}
}