XML响应

问题描述:

编辑

如何在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

那么我丹麦是不完美的(或目前在所有为此事)。但是,我知道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