色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

探討:array2xml和xml2array以及xml與array的互相轉(zhuǎn)化

php在做后臺(tái)服務(wù)器的時(shí)候,經(jīng)常會(huì)遇到這種情況,需要解析來(lái)自前臺(tái)的xml文件,并將數(shù)據(jù)以xml格式返回,在這種情況下,xml與php中關(guān)聯(lián)數(shù)組的轉(zhuǎn)化是非常頻繁的事情。比如flex和其他客戶端程序與服務(wù)器的交互,經(jīng)常會(huì)使用這種方法。下面是我歸納的兩個(gè)方法,大大簡(jiǎn)化了xml與數(shù)組相互轉(zhuǎn)化的工作量。
復(fù)制代碼 代碼如下:
/**
     *
     * 將簡(jiǎn)單數(shù)組轉(zhuǎn)化為簡(jiǎn)單的xml
     * @param string $data  要進(jìn)行轉(zhuǎn)化的數(shù)組
     * @param string $tag   要使用的標(biāo)簽
     * @example
     * $arr = array(
        'rtxAccount'=>'aaron','ipAddr'=>'192.168.0.12',
        'conferenceList'=>array('conference'=>
                            array(
                                array('conferenceId'=>1212,'conferenceTitle'=>'quanshi 444','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>454,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>6767,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>232323,'conferenceTitle'=>'quanshi uuu','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>8989,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.NET'),
                                array('conferenceId'=>1234343212,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.NET')
                                )
                            )
        );
        轉(zhuǎn)化為:
        <rtxAccount>aaron</rtxAccount>
        <ipAddr>192.168.0.12</ipAddr>
        <conferenceList>
            <conference>
                <conferenceId>1212</conferenceId>
                <conferenceTitle>quanshi 444</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>454</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>6767</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>232323</conferenceId>
                <conferenceTitle>quanshi uuu</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>8989</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>http://www.jb51.NET</smeAccount>
            </conference>
            <conference>
                <conferenceId>1234343212</conferenceId>
                <conferenceTitle>quanshi meetting</conferenceTitle>
                <smeAccount>http://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
復(fù)制代碼 代碼如下:
/**
  *
  * 將簡(jiǎn)單的xml轉(zhuǎn)化成關(guān)聯(lián)數(shù)組
  * @param string $xmlString  xml字符串
  * @example
  * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RTXConferenceReqDTO>
 <conferenceTitle>IT交流會(huì)</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>被邀請(qǐng)人1的RTX賬號(hào)</rtxAccount>
   <tel>被邀請(qǐng)人1電話號(hào)碼</tel>
  </invitee>
  <invitee>
   <rtxAccount>被邀請(qǐng)人2的RTX賬號(hào)</rtxAccount>
   <tel>被邀請(qǐng)人2電話號(hào)碼</tel>
  </invitee>
 </invitees>
</RTXConferenceReqDTO>
轉(zhuǎn)化之后的關(guān)聯(lián)數(shù)組:
Array
(
    [conferenceTitle] => IT交流會(huì)
    [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] => 被邀請(qǐng)人1的RTX賬號(hào)
                            [tel] => 被邀請(qǐng)人1電話號(hào)碼
                        )
                    [1] => Array
                        (
                            [rtxAccount] => 被邀請(qǐng)人2的RTX賬號(hào)
                            [tel] => 被邀請(qǐng)人2電話號(hào)碼
                        )
                )
        )
)
  */
 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技術(shù)探討:array2xml和xml2array以及xml與array的互相轉(zhuǎn)化,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 拜泉县| 青岛市| 宜丰县| 刚察县| 剑阁县| 应城市| 马边| 高淳县| 织金县| 贺州市| 仲巴县| 商水县| 大竹县| 麻城市| 巨鹿县| 汾西县| 和平县| 象山县| 那曲县| 淮安市| 潞西市| 阿鲁科尔沁旗| 临汾市| 馆陶县| 安义县| 安多县| 富锦市| 苏尼特左旗| 昆明市| 滨州市| 堆龙德庆县| 海原县| 瑞丽市| 广元市| 黑龙江省| 南澳县| 诸暨市| 万山特区| 大石桥市| 岐山县| 桓仁|