2013-02-18 58 views
0

例如我有一個數組副本陣列爲xml不能改變的XML標籤標題

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); 
      } 
     } 
    } 
} 
+1

您可以修改數組'(「汽車「=>」bmw「...)'在getXML()'之前。 – Passerby 2013-02-18 03:30:11

回答

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 
) 
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";