探讨: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 Session_Regenerate_ID函数双释放内存破坏漏洞
Jan 27 PHP
php中json_encode中文编码问题分析
Sep 13 PHP
基于PHP+Ajax实现表单验证的详解
Jun 25 PHP
php 获取页面中指定内容的实现类
Jan 23 PHP
PHP结合JQueryJcrop实现图片裁切实例详解
Jul 24 PHP
php修改上传图片尺寸的方法
Apr 14 PHP
Codeigniter控制器controller继承问题实例分析
Jan 19 PHP
PHP实现长文章分页实例代码(附源码)
Feb 03 PHP
Yii2中如何使用modal弹窗(基本使用)
May 30 PHP
PHP获取访问页面HTTP状态码的实现代码
Nov 03 PHP
php redis实现对200w用户的即时推送
Mar 04 PHP
PHP实现双链表删除与插入节点的方法示例
Nov 11 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
php实现简单的语法高亮函数实例分析
2015/04/27 PHP
初识PHP中的Swoole
2016/04/05 PHP
PHP实现通过CURL上传文件功能示例
2018/05/30 PHP
Laravel 微信小程序后端搭建步骤详解
2019/11/26 PHP
JS window.opener返回父页面的应用
2009/10/24 Javascript
javascript 面向对象全新理练之原型继承
2009/12/03 Javascript
window.showModalDialog参数传递中含有特殊字符的处理方法
2013/06/06 Javascript
select、radio表单回显功能实现避免使用jquery载入赋值
2013/06/08 Javascript
JQuery中使用Ajax赋值给全局变量失败异常的解决方法
2014/08/18 Javascript
jQuery自动完成插件completer附源码下载
2016/01/04 Javascript
基于jQuery实现Accordion手风琴自定义插件
2020/10/13 Javascript
详解微信小程序 相对定位和绝对定位
2017/05/11 Javascript
js+html5实现手机九宫格密码解锁功能
2018/07/30 Javascript
webpack file-loader和url-loader的区别
2019/01/15 Javascript
Vue代码整洁之去重方法整理
2019/08/06 Javascript
layui表格分页 记录勾选的实例
2019/09/02 Javascript
使用layui定义一个模块并使用的例子
2019/09/14 Javascript
[01:05:24]Ti4 冒泡赛第二天 iG vs NEWBEE 3
2014/07/15 DOTA
[53:23]Secret vs Liquid 2018国际邀请赛淘汰赛BO3 第二场 8.25
2018/08/29 DOTA
详解Python中DOM方法的动态性
2015/04/11 Python
使用python生成杨辉三角形的示例代码
2018/08/29 Python
Python中垃圾回收和del语句详解
2018/11/15 Python
Django实现一对多表模型的跨表查询方法
2018/12/18 Python
pytorch 实现查看网络中的参数
2020/01/06 Python
Python numpy矩阵处理运算工具用法汇总
2020/07/13 Python
墨西哥网上购物:Linio墨西哥
2016/10/20 全球购物
Omio法国:全欧洲低价大巴、火车和航班搜索和比价
2017/11/13 全球购物
运动会开幕式邀请函
2014/02/03 职场文书
体育教师个人的自我评价
2014/02/16 职场文书
《登鹳雀楼》教学反思
2014/04/09 职场文书
县级文明单位申报材料
2014/05/23 职场文书
试用期员工工作自我评价
2014/09/10 职场文书
学习群众路线的心得体会
2014/11/05 职场文书
工作感言一句话
2015/08/01 职场文书
陶瓷类经典广告语集锦
2019/10/25 职场文书
anaconda python3.8安装后降级
2021/06/11 Python