探讨: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目录导航文件代码
Oct 09 PHP
PHP开发中的错误收集,不定期更新。
Feb 03 PHP
php中将一段数据存到一个txt文件中并显示其内容
Aug 15 PHP
PHP+jQuery 注册模块开发详解
Oct 14 PHP
推荐5款跨平台的PHP编辑器
Dec 25 PHP
PHP浮点数精度问题汇总
May 13 PHP
PHP获取Exif缩略图的方法
Jul 13 PHP
PHP5.3连接Oracle客户端及PDO_OCI模块的安装方法
May 13 PHP
浅谈php处理后端&amp;接口访问超时的解决方法
Oct 29 PHP
php实现的统计字数函数定义与使用示例
Jul 26 PHP
实例讲解PHP页面静态化
Feb 05 PHP
PHP通过get方法获得form表单数据方法总结
Sep 12 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中使用sockets:从新闻组中获取文章
2006/10/09 PHP
PHP 日期时间函数的高级应用技巧
2009/10/10 PHP
几款免费开源的不用数据库的php的cms
2010/12/19 PHP
php安装xdebug/php安装pear/phpunit详解步骤(图)
2013/12/22 PHP
php表单提交与$_POST实例分析
2015/01/26 PHP
PHP 搜索查询功能实现
2016/11/29 PHP
掌握PHP垃圾回收机制详解
2019/03/13 PHP
IE JS编程需注意的内存释放问题
2009/06/23 Javascript
js去除输入框中所有的空格和禁止输入空格的方法
2014/06/09 Javascript
js调试系列 初识控制台
2014/06/18 Javascript
JavaScript数组常用操作技巧汇总
2014/11/17 Javascript
JavaScript中神奇的call()方法
2015/03/12 Javascript
AngularJS 简单应用实例
2016/07/28 Javascript
Vue+Element使用富文本编辑器的示例代码
2017/08/14 Javascript
深入浅出webpack之externals的使用
2017/12/04 Javascript
js生成word中图片处理方法
2018/01/06 Javascript
vue.js绑定事件监听器示例【基于v-on事件绑定】
2018/07/07 Javascript
jQuery事件多次绑定与解绑问题实例分析
2019/02/19 jQuery
jQuery-App输入框实现实时搜索
2020/11/19 jQuery
[51:26]VP vs VG 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
跟老齐学Python之数据类型总结
2014/09/24 Python
Python2.x中文乱码问题解决方法
2015/06/02 Python
探究python中open函数的使用
2016/03/01 Python
python绘制中国大陆人口热力图
2018/11/07 Python
python3.6 tkinter实现屏保小程序
2019/07/30 Python
Python 使用matplotlib模块模拟掷骰子
2019/08/08 Python
使用Python实现正态分布、正态分布采样
2019/11/20 Python
pyinstaller打包找不到文件的问题解决
2020/04/15 Python
介绍一下.NET构架下remoting和webservice
2014/05/08 面试题
医学专业毕业生个人求职信
2013/12/25 职场文书
晚归检讨书
2014/02/19 职场文书
个人委托书范本
2014/04/02 职场文书
升旗仪式演讲稿
2014/05/08 职场文书
2015年新农村建设指导员工作总结
2015/07/24 职场文书
团队合作精神学习心得体会
2016/01/19 职场文书
MySQL创建高性能索引的全步骤
2021/05/02 MySQL