欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

探讨:array2xml和xml2array以及xml与array的互相转化

程序员文章站 2022-05-11 17:50:24
php在做后台服务器的时候,经常会遇到这种情况,需要解析来自前台的xml文件,并将数据以xml格式返回,在这种情况下,xml与php中关联数组的转化是非常频繁的事情。比如f...

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'=>'//www.jb51.net'),
                                array('conferenceid'=>454,'conferencetitle'=>'quanshi meetting','smeaccount'=>'//www.jb51.net'),
                                array('conferenceid'=>6767,'conferencetitle'=>'quanshi meetting','smeaccount'=>'//www.jb51.net'),
                                array('conferenceid'=>232323,'conferencetitle'=>'quanshi uuu','smeaccount'=>'//www.jb51.net'),
                                array('conferenceid'=>8989,'conferencetitle'=>'quanshi meetting','smeaccount'=>'//www.jb51.net'),
                                array('conferenceid'=>1234343212,'conferencetitle'=>'quanshi meetting','smeaccount'=>'//www.jb51.net')
                                )
                            )
        );
        转化为:
        <rtxaccount>aaron</rtxaccount>
        <ipaddr>192.168.0.12</ipaddr>
        <conferencelist>
            <conference>
                <conferenceid>1212</conferenceid>
                <conferencetitle>quanshi 444</conferencetitle>
                <smeaccount>//www.jb51.net</smeaccount>
            </conference>
            <conference>
                <conferenceid>454</conferenceid>
                <conferencetitle>quanshi meetting</conferencetitle>
                <smeaccount>//www.jb51.net</smeaccount>
            </conference>
            <conference>
                <conferenceid>6767</conferenceid>
                <conferencetitle>quanshi meetting</conferencetitle>
                <smeaccount>//www.jb51.net</smeaccount>
            </conference>
            <conference>
                <conferenceid>232323</conferenceid>
                <conferencetitle>quanshi uuu</conferencetitle>
                <smeaccount>//www.jb51.net</smeaccount>
            </conference>
            <conference>
                <conferenceid>8989</conferenceid>
                <conferencetitle>quanshi meetting</conferencetitle>
                <smeaccount>//www.jb51.net</smeaccount>
            </conference>
            <conference>
                <conferenceid>1234343212</conferenceid>
                <conferencetitle>quanshi meetting</conferencetitle>
                <smeaccount>//www.jb51.net</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;

 }