探讨: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实现用户认证及管理完全源码
Mar 11 PHP
php中通过curl smtp发送邮件
Jun 05 PHP
用PHP和Shell写Hadoop的MapReduce程序
Apr 15 PHP
一个php生成16位随机数的代码(两种方法)
Sep 16 PHP
php使用pdo连接并查询sql数据库的方法
Dec 24 PHP
php实现TCP端口检测的方法
Apr 01 PHP
php+js实现百度地图多点标注的方法
Nov 30 PHP
php中引用&amp;的用法分析【变量引用,函数引用,对象引用】
Dec 12 PHP
php pdo操作数据库示例
Mar 10 PHP
php转换上传word文件为PDF的方法【基于COM组件】
Jun 10 PHP
基于laravel where的高级使用方法
Oct 10 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中支持多种编码的中文字符串截取函数!
2007/03/20 PHP
PHP如何解决网站大流量与高并发的问题
2011/06/25 PHP
php简单操作mysql数据库的类
2015/04/16 PHP
Yii2增删改查之查询 where参数详细介绍
2016/08/08 PHP
PHP实现打包zip并下载功能
2018/06/12 PHP
php常用经典函数集锦【数组、字符串、栈、队列、排序等】
2019/08/23 PHP
如何实现动态删除javascript函数
2007/05/27 Javascript
jquery.validate使用攻略 第一部
2010/07/01 Javascript
javascript实现鼠标拖动改变层大小的方法
2015/04/30 Javascript
jQuery横向擦除焦点图特效代码分享
2015/09/06 Javascript
JS实现模拟百度搜索“2012世界末日”网页地震撕裂效果代码
2015/10/31 Javascript
js实现网页收藏功能
2015/12/17 Javascript
jquery分页插件jquery.pagination.js实现无刷新分页
2016/04/01 Javascript
nodejs多版本管理总结
2018/04/03 NodeJs
vue.js 实现点击展开收起动画效果
2018/07/07 Javascript
vue-cli3.0配置及使用注意事项详解
2018/09/05 Javascript
微信小程序模板消息推送的两种实现方式
2019/08/27 Javascript
如何正确理解vue中的key详解
2019/11/02 Javascript
简单了解JavaScript arguement原理及作用
2020/05/28 Javascript
[10:28]2018DOTA2国际邀请赛寻真——VGJ.S寻梦之路
2018/08/15 DOTA
Python使用xlrd读取Excel格式文件的方法
2015/03/10 Python
Python调用SQLPlus来操作和解析Oracle数据库的方法
2016/04/09 Python
Python有序查找算法之二分法实例分析
2017/12/11 Python
python在文本开头插入一行的实例
2018/05/02 Python
pycharm 配置远程解释器的方法
2018/10/28 Python
Python操作redis实例小结【String、Hash、List、Set等】
2019/05/16 Python
python 实现图片批量压缩的示例
2020/12/18 Python
HTML5 audio标签使用js进行播放控制实例
2015/04/24 HTML / CSS
森海塞尔美国官网:Sennheiser耳机与耳麦
2017/07/19 全球购物
英国时尚运动品牌的合集:The Sports Edit
2017/12/20 全球购物
2019年Java 最常见的 面试题
2016/10/19 面试题
五一劳动节活动记录
2014/03/23 职场文书
学校评语大全
2014/05/06 职场文书
投诉书格式范本
2015/07/02 职场文书
vue前端工程的搭建
2021/03/31 Vue.js
Zabbix对Kafka topic积压数据监控的问题(bug优化)
2022/07/07 Servers