0

例如我有一个数组

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);
            }
        }
    }
}
4

2 回答 2

0

您需要将每两个值配对以获取要添加的元素的名称和值。例如,您可以这样做array_chunk

$data  = Array(0 => 'car', 1 => 'bmw', 2 => 'colour', 3 => 'red', 4 => 'quality', 5 => 'good');
$count = count($data);
if ($count % 2) {
    throw new InvalidArgumentException('Array not of pairs.');
}

$xml = new XMLWriter();
$xml->openMemory();
$xml->startDocument();
$xml->startElement('root');

foreach (array_chunk($data, 2) as $pair) {
    list($name, $value) = $pair;
    $xml->writeElement($name, $value);
}

$xml->endElement();
echo $xml->outputMemory();

由于您无论如何都在使用内存,因此您也可以使用 SimpleXML 来执行此操作:

$data  = Array(0 => 'car', 1 => 'bmw', 2 => 'colour', 3 => 'red', 4 => 'quality', 5 => 'good');
$count = count($data);
if ($count % 2) {
    throw new InvalidArgumentException('Array not of pairs.');
}

$xml = new SimpleXMLElement('<root/>');

foreach (array_chunk($data, 2) as $pair) {
    list($name, $value) = $pair;
    $xml->{$name}[] = $value;
}

echo $xml->asXML(), "\n";
于 2013-02-18T08:49:30.310 回答
0

在您的convert($data)方法中,更改以下部分:

if (is_array($data)) {
    $this->getXML($data);
}

至:

if(is_array($data))
{
    if(count($data)%2===0)
    {
        $cache=array();
        while(count($data)>0)
        {
            list($key,$val)=array_splice($data,0,2);
            $cache[$key]=$val;
        }
        print_r($cache);//just to debug
        $this->getXML($cache);
    }
    else
    {
        $this->getXML($data);
    }
}

我没有测试你的整个班级,但对于操纵部分,我确实测试了:

$data=array("car","bmw","colour","red","quality","good");
if(count($data)%2===0)
/* ...... */
print_r($cache);

上面的代码输出:

Array
(
    [car] => bmw
    [colour] => red
    [quality] => good
)
于 2013-02-18T03:39:40.180 回答