探讨: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和js交互一例-PHP教程,PHP应用
Jan 03 PHP
表单复选框向PHP传输数据的代码
Nov 13 PHP
PHP学习笔记之字符串编码的转换和判断
May 22 PHP
php常用hash加密函数
Nov 22 PHP
apache中为php 设置虚拟目录
Dec 17 PHP
WordPress中&quot;无法将上传的文件移动至&quot;错误的解决方法
Jul 01 PHP
php查询操作实现投票功能
May 09 PHP
[原创]PHP实现SQL语句格式化功能的方法
Jul 28 PHP
PHP多线程模拟实现秒杀抢单
Feb 07 PHP
PHP封装的page分页类定义与用法完整示例
Dec 24 PHP
laravel执行php artisan migrate报错的解决方法
Oct 09 PHP
如何在PHP中生成随机数
Jun 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
php echo()和print()、require()和include()函数区别说明
2010/03/27 PHP
php中spl_autoload详解
2014/10/17 PHP
php支付宝在线支付接口开发教程
2016/09/19 PHP
PHP PDOStatement::errorInfo讲解
2019/01/31 PHP
PHP token验证生成原理实例分析
2019/06/05 PHP
jQuery 可以拖动的div实现代码 脚本之家修正版
2009/06/26 Javascript
Jquery Ajax.ashx 高效分页实现代码
2009/10/20 Javascript
javascript 中String.match()与RegExp.exec()的区别说明
2013/01/10 Javascript
jQuery+CSS实现菜单滑动伸展收缩(仿淘宝)
2013/03/22 Javascript
主页面中的两个iframe实现鼠标拖动改变其大小
2013/04/16 Javascript
js简单实现标签云效果实例
2015/08/06 Javascript
js实现基于正则表达式的轻量提示插件
2015/08/29 Javascript
Js制作点击输入框时默认文字消失的效果
2015/09/05 Javascript
JS实现线性表的链式表示方法示例【经典数据结构】
2017/04/11 Javascript
Angular2使用Angular CLI快速搭建工程(一)
2017/05/21 Javascript
JavaScript实现QQ聊天消息展示和评论提交功能
2017/05/22 Javascript
JS设计模式之观察者模式实现实时改变页面中金额数的方法
2018/02/05 Javascript
微信小程序和百度的语音识别接口详解
2019/05/06 Javascript
使用vuex较为优雅的实现一个购物车功能的示例代码
2019/12/09 Javascript
详解Python中contextlib上下文管理模块的用法
2016/06/28 Python
Django实战之用户认证(初始配置)
2018/07/16 Python
TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片
2019/03/14 Python
用python打印菱形的实操方法和代码
2019/06/25 Python
python调用matplotlib模块绘制柱状图
2019/10/18 Python
python logging设置level失败的解决方法
2020/02/19 Python
Python requests模块session代码实例
2020/04/14 Python
pycharm 对代码做静态检查操作
2020/06/09 Python
请用用Java代码写一个堆栈
2012/01/26 面试题
如何定义一个可复用的服务
2014/09/30 面试题
信用社员工先进事迹材料
2014/02/04 职场文书
音乐剧猫观后感
2015/06/04 职场文书
2016年春节问候语
2015/11/11 职场文书
执行力心得体会范文
2016/01/11 职场文书
2020年元旦晚会策划书模板
2019/12/30 职场文书
Python开发简易五子棋小游戏
2022/05/02 Python
讲解MySQL增删改操作
2022/05/06 MySQL