探讨:array2xml和xml2array以及xml与array的互相转化


Posted in PHP onJune 24, 2013

php在做后台服务器的时候,经常会遇到这种情况,需要解析来自前台的xml文件,并将数据以xml格式返回,在这种情况下,xml与php中关联数组的转化是非常频繁的事情。比如flex和其他客户端程序与服务器的交互,经常会使用这种方法。下面是我归纳的两个方法,大大简化了xml与数组相互转化的工作量。

/**
     *
     * 将简单数组转化为简单的xml
     * @param string $data  要进行转化的数组
     * @param string $tag   要使用的标签
     * @example
     * $arr = array(
        'rtxAccount'=>'aaron','ipAddr'=>'192.168.0.12',
        'conferenceList'=>array('conference'=>
                            array(
                                array('conferenceId'=>1212,'conferenceTitle'=>'quanshi 444','smeAccount'=>'https://3water.com'),
                                array('conferenceId'=>454,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'https://3water.com'),
                                array('conferenceId'=>6767,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'https://3water.com'),
                                array('conferenceId'=>232323,'conferenceTitle'=>'quanshi uuu','smeAccount'=>'https://3water.com'),
                                array('conferenceId'=>8989,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'https://3water.com'),
                                array('conferenceId'=>1234343212,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'https://3water.com')
                                )
                            )
        );
        转化为:
        <rtxAccount>aaron</rtxAccount>
        <ipAddr>192.168.0.12</ipAddr>
        <conferenceList>
            <conference>
                <conferenceId>1212</conferenceId>
                <conferenceTitle>quanshi 444</conferenceTitle>
                <smeAccount>https://3water.com</smeAccount>
            </conference>
            <conference>
                <conferenceId>454</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>https://3water.com</smeAccount>
            </conference>
            <conference>
                <conferenceId>6767</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>https://3water.com</smeAccount>
            </conference>
            <conference>
                <conferenceId>232323</conferenceId>
                <conferenceTitle>quanshi uuu</conferenceTitle>
                <smeAccount>https://3water.com</smeAccount>
            </conference>
            <conference>
                <conferenceId>8989</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>https://3water.com</smeAccount>
            </conference>
            <conference>
                <conferenceId>1234343212</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>https://3water.com</smeAccount>
            </conference>
        </conferenceList>
     */
    function array2xml($data,$tag = '')
    {
        $xml = '';        foreach($data as $key => $value)
        {
            if(is_numeric($key))
            {
                if(is_array($value))
                {
                    $xml .= "<$tag>";
                    $xml .= array2xml($value);
                    $xml .="</$tag>";
                }
                else
                {
                    $xml .= "<$tag>$value</$tag>";
                }    
            }
            else
            {
                if(is_array($value))
                {
                    $keys = array_keys($value);
                    if(is_numeric($keys[0]))
                    {
                        $xml .=array2xml($value,$key);
                    }
                    else
                    {
                        $xml .= "<$key>";
                        $xml .=array2xml($value);
                        $xml .= "</$key>";
                    }
                }
                else
                {
                    $xml .= "<$key>$value</$key>";
                }
            }
        }
        return $xml;
    }             
}

xml2array
/**
  * 
  * 将简单的xml转化成关联数组
  * @param string $xmlString  xml字符串
  * @example
  * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RTXConferenceReqDTO>
 <conferenceTitle>IT交流会</conferenceTitle>
 <startTime>2011-12-19 12:00:00</startTime>
 <rtxAccount>andy1111111</rtxAccount>
 <ipAddr>192.168.1.56</ipAddr>
 <duration>120</duration>
 <conferenceType>1</conferenceType>
 <invitees>
  <invitee>
   <rtxAccount>被邀请人1的RTX账号</rtxAccount>
   <tel>被邀请人1电话号码</tel>
  </invitee>
  <invitee>
   <rtxAccount>被邀请人2的RTX账号</rtxAccount>
   <tel>被邀请人2电话号码</tel>
  </invitee>
 </invitees>
</RTXConferenceReqDTO>
转化之后的关联数组:
Array
(
    [conferenceTitle] => IT交流会
    [startTime] => 2011-12-19 12:00:00
    [rtxAccount] => andy1111111
    [ipAddr] => 192.168.1.56
    [duration] => 120
    [conferenceType] => 1
    [invitees] => Array
        (
            [invitee] => Array
                (
                    [0] => Array
                        (
                            [rtxAccount] => 被邀请人1的RTX账号
                            [tel] => 被邀请人1电话号码
                        )
                    [1] => Array
                        (
                            [rtxAccount] => 被邀请人2的RTX账号
                            [tel] => 被邀请人2电话号码
                        )
                )
        )
)
  */
 function xml2array($xmlString = '')
 {
  $targetArray = array();
  $xmlObject = simplexml_load_string($xmlString);
  $mixArray = (array)$xmlObject;
  foreach($mixArray as $key => $value)
  {
   if(is_string($value))
   {
    $targetArray[$key] = $value;
   }
   if(is_object($value))
   {
    $targetArray[$key] = xml2array($value->asXML());
   }
   if(is_array($value))
   {
    foreach($value as $zkey => $zvalue)
    {
     if(is_numeric($zkey))
     {
      $targetArray[$key][] = xml2array($zvalue->asXML());
     }
     if(is_string($zkey))
     {
      $targetArray[$key][$zkey] = xml2array($zvalue->asXML());
     }
    }
   }
  }
  return $targetArray; }

PHP 相关文章推荐
初级的用php写的采集程序
Mar 16 PHP
PHP网站备份程序代码分享
Jun 10 PHP
php简单开启gzip压缩方法(zlib.output_compression)
Apr 13 PHP
php变量作用域的深入解析
Jun 03 PHP
php正则preg_replace_callback函数用法实例
Jun 01 PHP
Linux操作系统安装LAMP环境
Jun 26 PHP
详谈PHP编码转换问题
Jul 28 PHP
PHP进程通信基础之信号量与共享内存通信
Feb 19 PHP
PHP自动补全表单的两种方法
Mar 06 PHP
利用laravel+ajax实现文件上传功能方法示例
Aug 13 PHP
PHP分页显示的方法分析【附PHP通用分页类】
May 10 PHP
PHP设计模式之装饰器模式定义与用法简单示例
Aug 13 PHP
解析Ubuntu下crontab命令的用法
Jun 24 #PHP
关于crontab的使用详解
Jun 24 #PHP
解析PHPExcel使用的常用说明以及把PHPExcel整合进CI框架的介绍
Jun 24 #PHP
关于Zend Studio 配色方案插件的介绍
Jun 24 #PHP
解析argc argv在php中的应用
Jun 24 #PHP
解析func_num_args与func_get_args函数的使用
Jun 24 #PHP
php常用ODBC函数集(详细)
Jun 24 #PHP
You might like
虹吸式咖啡探讨–研磨
2021/03/03 冲泡冲煮
php5编程中的异常处理详细方法介绍
2008/07/29 PHP
laravel 数据验证规则详解
2019/10/23 PHP
jquery validate.js表单验证的基本用法入门
2010/05/13 Javascript
在次封装easyui-Dialog插件实现代码
2010/11/14 Javascript
jquery通过visible来判断标签是否显示或隐藏
2014/05/08 Javascript
教你用jquery实现iframe自适应高度
2014/06/11 Javascript
详解js闭包
2014/09/02 Javascript
Json解析的方法小结
2016/06/22 Javascript
Google 地图API Map()构造器详解
2016/08/06 Javascript
NodeJS处理Express中异步错误
2017/03/26 NodeJs
BootStrap的双日历时间控件使用
2017/07/25 Javascript
实例解析Vue.js下载方式及基本概念
2018/05/11 Javascript
Vue中的组件及路由使用实例代码详解
2019/05/22 Javascript
微信小程序的开发范式BeautyWe.js入门详解
2019/07/10 Javascript
js实现点击图片在屏幕中间弹出放大效果
2019/09/11 Javascript
vue中根据时间戳判断对应的时间(今天 昨天 前天)
2019/12/20 Javascript
javascript History对象原理解析
2020/02/17 Javascript
详解Vue.js 响应接口
2020/07/04 Javascript
[15:23]教你分分钟做大人:虚空假面
2014/10/30 DOTA
Python程序设计入门(2)变量类型简介
2014/06/16 Python
Python中的zip函数使用示例
2015/01/29 Python
Django1.7+python 2.78+pycharm配置mysql数据库
2016/10/09 Python
Python实现自动登录百度空间的方法
2017/06/10 Python
分享几道你可能遇到的python面试题
2017/07/24 Python
django使用xlwt导出excel文件实例代码
2018/02/06 Python
Python实现简单http服务器
2018/04/12 Python
python取数作为临时极大值(极小值)的方法
2018/10/15 Python
Python3 log10()函数简单用法
2019/02/19 Python
python中的TCP(传输控制协议)用法实例分析
2019/11/15 Python
canvas因为图片资源不在同一域名下而导致的跨域污染画布的解决办法
2019/01/18 HTML / CSS
英国男女奢华内衣和泳装购物网站:Figleaves
2017/01/28 全球购物
2015年世界无烟日活动方案
2015/05/04 职场文书
创新创业项目计划书该怎样写?
2019/08/13 职场文书
CSS3 制作的图片滚动效果
2021/04/14 HTML / CSS
css3 实现文字闪烁效果的三种方式示例代码
2021/04/25 HTML / CSS