2011-07-04 48 views
0

編輯

如何在XML 「頭」 加編碼XML響應

現在的 「頭」 是這樣的:

<?xml version="1.0"?> 

你怎麼能使用SimpleXML時在PHP中添加一個編碼在「標題」?

我的XML類:

class XML { 
    private $root = '<response />'; 

    function __construct($root=null){ 
     $this->root = new SimpleXMLElement($root ? $root:$this->root); 
    } 

    function encode($arr, $node=null){ 
     $node = $node == null ? $this->root:$node; 
     foreach($arr as $key => $value){ 
      if(is_array($value)){ 
       $this->encode($value, $node->addChild($key)); 
      } 
      else{ 
       $node->addChild($key, $value); 
      } 
     } 
    } 

    function output(){ 
     return $this->root->asXML(); 
    } 
} 
+0

什麼是錯誤您收到? – jerluc

+0

在我的問題看看 – clarkk

回答

4

那麼我丹麥是不完美的(或目前在所有爲此事)。但是,我知道XML節點cannot begin with a number。由於錯誤似乎是抱怨XML數據的格式良好,我想說這是因爲<0></0>節點。

你可以嘗試改變:

private $root = '<response />'; 

爲了這樣的事情:

private $root = '<?xml version="1.0" encoding="utf-8" ?><response />'; 
+0

好..即時通訊新的xml ..一直使用json ..但你怎麼能寫一個數組在XML然後呢?只是在數組序列/索引之前放一個名字? – clarkk

+0

XML中的「數組」實際上是一組具有相同節點名稱的嵌套同級元素。 – jerluc

+0

是的,當然..嘿嘿:) – clarkk