zoukankan      html  css  js  c++  java
  • xml方式封装数据方法

    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>

    运行结果:

  • 相关阅读:
    css和js实现硬件加速渲染自定义滚动条
    入驻博客园
    原生JS实现动态折线图
    纯JS自定义网页滚动条
    Unity实现代码控制音频播放
    MVC实现修改数据
    JS实现将二维数组生成到页面上
    JS实现页面计时
    多中方式,实现斐波那契数列
    算法题
  • 原文地址:https://www.cnblogs.com/yangzailu/p/9195839.html
Copyright © 2011-2022 走看看