たとえば、私は配列を持っています
Array ( [0] => car [1] => bmw [2] => colour [3] => red [4] => quality [5] => good)
配列をxmlにコピーするクラスもあります。
このように配列をxmlにコピーします
<root>
<0>car</0>
<1>bmw</1>
<2>colour</2>
<3>red</3>
<4>quality</4>
<5>good</5>
</root>
このように配列をxmlにコピーする必要があります
<root>
<car>bmw</car>
<colour>red</colour>
<quality>good</quality>
</root>
私のクラス
class Array2XML {
private $writer;
private $version = '1.0';
private $encoding = 'UTF-8';
private $rootName = 'root';
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);
}
}
}
}