探讨: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 相关文章推荐
WINDOWS服务器安装多套PHP的另类解决方案
Oct 09 PHP
PHP 存储文本换行实现方法
Jan 05 PHP
eaglephp使用微信api接口开发微信框架
Jan 09 PHP
浅析PHP微信支付通知的处理方式
May 25 PHP
Zend Framework 2.0事件管理器(The EventManager)入门教程
Aug 11 PHP
laravel安装和配置教程
Oct 29 PHP
PHP实现中文圆形印章特效
Jun 19 PHP
CentOS下搭建PHP环境与WordPress博客程序的全流程总结
May 07 PHP
PHP实现防盗链的方法分析
Jul 25 PHP
PHP基于curl模拟post提交json数据示例
Jun 22 PHP
Yii框架的redis命令使用方法简单示例
Oct 15 PHP
Laravel 5.1 框架Blade模板引擎用法实例分析
Jan 04 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/04 无线电
php截取中文字符串函数实例
2015/02/23 PHP
浅谈PHP中单引号和双引号到底有啥区别呢?
2015/03/04 PHP
PHP生成随机密码4种方法及性能对比
2020/12/11 PHP
BOOM vs RR BO5 第四场 2.14
2021/03/10 DOTA
学习YUI.Ext 第四天--对话框Dialog的使用
2007/03/10 Javascript
jquery BS,dialog控件自适应大小
2009/07/06 Javascript
深入理解JavaScript定时机制
2010/10/29 Javascript
js判断屏幕分辨率的代码
2013/07/16 Javascript
JS操作CSS随机改变网页背景实现思路
2014/03/10 Javascript
利用a标签自动解析URL分析网址实例
2014/10/20 Javascript
node.js中的http.createServer方法使用说明
2014/12/14 Javascript
nodejs和php实现图片访问实时处理
2017/01/05 NodeJs
vue.js的安装方法
2017/05/12 Javascript
React-Native中禁用Navigator手势返回的示例代码
2017/09/09 Javascript
js实现复制功能(多种方法集合)
2018/01/06 Javascript
Node.js如何优雅的封装一个实用函数的npm包的方法
2019/04/29 Javascript
简单了解vue.js数组的常用操作
2019/06/17 Javascript
vue子路由跳转实现tab选项卡
2019/07/24 Javascript
Node.js实现简单管理系统
2019/09/23 Javascript
[02:36]DOTA2亚洲邀请赛小组赛精彩集锦:EE凭借法力虚空拿下4杀
2017/03/30 DOTA
python在Windows8下获取本机ip地址的方法
2015/03/14 Python
Python3实现对列表按元组指定列进行排序的方法分析
2018/12/22 Python
python 产生token及token验证的方法
2018/12/26 Python
python+selenium 点击单选框-radio的实现方法
2019/09/03 Python
python3 pathlib库Path类方法总结
2019/12/26 Python
利用PyQt中的QThread类实现多线程
2020/02/18 Python
pycharm全局搜索的具体步骤
2020/07/28 Python
Microsoft新加坡官方网站:购买微软最新软件和技术产品
2016/10/28 全球购物
Martinelli官方商店:西班牙皮鞋和高跟鞋品牌
2019/07/30 全球购物
慕尼黑山地运动、户外服装和体育用品专家:Sporthaus Schuster
2019/08/27 全球购物
Janie and Jack美国官网:GAP旗下的高档童装品牌
2019/09/09 全球购物
业务经理的岗位职责
2013/11/16 职场文书
房屋租赁授权委托书范本
2014/09/20 职场文书
五年级作文之劳动作文
2019/11/12 职场文书
Python代码实现双链表
2022/05/25 Python