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'=>'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')
)
)
);
轉化為:
<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
復制代碼 代碼如下:
/**
*
* 將簡單的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;
}