开发者

XML response on REST API

edit

how to add a encoding in the xml "header"

now the "header" looks like this:

<?xml version="1.0"?>
开发者_开发技巧

how can you when using SimpleXML in PHP add a encoding in the "header"?

my xml class:

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


Well my Danish isn't perfect (or present at all for that matter). However, I know as a fact XML nodes cannot begin with a number. And since the error seems to be complaining abou the well-formedness of the XML data, I'd say it's because of the <0></0> node.

You can try to change:

private $root = '<response />';

To something like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜