Home  >  Article  >  Backend Development  >  About the method of encapsulating data in xml

About the method of encapsulating data in xml

不言
不言Original
2018-07-04 16:32:372344browse

This article mainly introduces the method of encapsulating data in xml, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

  1. xml encapsulation Data method

##2.demo

$code,
        'message'=>$message,
        'data'=>$data,
        );
    header("Content-Type:text/html");
    $xml ="";
    $xml .="";
    $xml .=self::xmlToEncode($result);
    $xml .="";

 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 .="";    
    }
    return $xml;

}
$data = array(
    'id'=>1,
    'name'=>'xinlang',
    'type'=>array(),
    );
Response::xmlEncode(200,'success',$data);


//注意 xml的节点不能为数字,如果传默认数组需要处理下标值

Running result:

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to configure independent subdomain names in the yii backend

The above is the detailed content of About the method of encapsulating data in xml. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn