探讨: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 相关文章推荐
example2.php
Oct 09 PHP
PHP 分页原理分析,大家可以看看
Dec 21 PHP
获取用户Ip地址通用方法与常见安全隐患(HTTP_X_FORWARDED_FOR)
Jun 01 PHP
深入理解require与require_once与include以及include_once的区别
Jun 05 PHP
PHP提交表单失败后如何保留已经填写的信息
Jun 20 PHP
thinkphp实现上一篇与下一篇的方法
Dec 08 PHP
如何直接访问php实例对象中的private属性详解
Oct 12 PHP
php之header的不同用法总结(实例讲解)
Nov 28 PHP
PHP如何实现阿里云短信sdk灵活应用在项目中的方法
Jun 14 PHP
tp5修改(实现即点即改)
Oct 18 PHP
laravel 解决多库下的DB::transaction()事务失效问题
Oct 21 PHP
PHP fopen中文文件名乱码问题解决方案
Oct 28 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 JSON 数据解析代码
2010/05/26 PHP
php设计模式 Interpreter(解释器模式)
2011/06/26 PHP
php的$_FILES的临时储存文件与回收机制实测过程
2013/07/12 PHP
php读取目录及子目录下所有文件名的方法
2014/10/20 PHP
PHP获取数组的键与值方法小结
2015/06/13 PHP
php实现网页端验证码功能
2017/07/11 PHP
laravel admin实现分类树/模型树的示例代码
2020/06/10 PHP
JS模拟多线程
2007/02/07 Javascript
js onkeypress与onkeydown 事件区别详细说明
2012/12/13 Javascript
使用jquery自定义鼠标样式满足个性需求
2013/11/05 Javascript
JS实现的通用表单验证插件完整实例
2015/08/20 Javascript
js removeChild 方法深入理解
2016/08/16 Javascript
AngularJs 常用的过滤器
2017/05/15 Javascript
原生JavaScript实现的简单省市县三级联动功能示例
2017/05/27 Javascript
jquery仿京东商品放大浏览页面
2017/06/06 jQuery
原生JS 购物车及购物页面的cookie使用方法
2017/08/21 Javascript
nodejs实现超简单生成二维码的方法
2018/03/17 NodeJs
Vue中使用的EventBus有生命周期
2018/07/12 Javascript
Python实现批量把SVG格式转成png、pdf格式的代码分享
2014/08/21 Python
实例探究Python以并发方式编写高性能端口扫描器的方法
2016/06/14 Python
从django的中间件直接返回请求的方法
2018/05/30 Python
python3学生名片管理v2.0版
2018/11/29 Python
python多个模块py文件的数据共享实例
2019/01/11 Python
PyQt5基本控件使用之消息弹出、用户输入、文件对话框的使用方法
2019/08/06 Python
PyCharm取消波浪线、下划线和中划线的实现
2020/03/03 Python
Python3开发实例之非关系型图数据库Neo4j安装方法及Python3连接操作Neo4j方法实例
2020/03/18 Python
K近邻法(KNN)相关知识总结以及如何用python实现
2021/01/28 Python
表单button的outline在firefox浏览器下的问题
2012/12/24 HTML / CSS
html5 application cache遇到的严重问题
2012/12/26 HTML / CSS
进程的查看和调度分别使用什么命令
2015/03/25 面试题
公务员职务工作的自我评价
2013/11/01 职场文书
一名毕业生的自我鉴定
2013/12/04 职场文书
护士求职自荐信范文
2014/03/19 职场文书
离退休人员聘用协议书
2014/11/24 职场文书
2016年度继续教育学习心得体会
2016/01/19 职场文书
MySQL 8.0 Online DDL快速加列的相关总结
2021/06/02 MySQL