xml方式封装数据方法
发布时间:2020-12-16 23:32:46 所属栏目:百科 来源:网络整理
导读:1.xml方式封装数据方法 2.demo ? phpxml方式封装数据方法/** * [xmlEncode description] * @param [type] $code [description] * @param [type] $message [description] * @param array $data [description] * @return [type] [description] */public static
1.xml方式封装数据方法 2.demo <?php xml方式封装数据方法 /** * [xmlEncode description] * @param [type] $code [description] * @param [type] $message [description] * @param array $data [description] * @return [type] [description] */ public static function xmlEncode($code,$message,$data= array()){ if(!is_numeric($code)){ return; } $result = array( ‘code‘=>$code,‘message‘=>$message,‘data‘=>$data,); header("Content-Type:text/html"); $xml ="<?xml version=‘1.0‘ encoding=‘UTF-8‘>"; $xml .="<root>"; $xml .=self::xmlToEncode($result); $xml .="</root>"; echo $xml; } public static function xmlToEncode($data){ $xml = $attr ""; foreach ($data as $key => $value) { //xml的节点不能为数字,如果传默认数组需要处理下标值 if(is_numeric($key)){ $attr = "id=‘{$key}‘"; $key = "item"; } $xml .="<{$key}>"; $xml .=is_array($value)?self::xmlToEncode($value):$value; $xml .="</{$key}>"; } return $xml; } $data = array( ‘id‘=>1,‘name‘=>‘xinlang‘,‘type‘=>array(),); Response::xmlEncode(200,‘success‘,$data); //注意 xml的节点不能为数字,如果传默认数组需要处理下标值 <item id="0"></item> 运行结果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |