时间:2021-07-01 10:21:17 帮助过:3人阅读
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>运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
yii 后台配置独立子域名的方法
以上就是关于xml方式封装数据的方法的详细内容,更多请关注Gxl网其它相关文章!