探討: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)化的工作量。
/**
*
* 將簡(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.dbjr.com.cn'),
array('conferenceId'=>454,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.dbjr.com.cn'),
array('conferenceId'=>6767,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.dbjr.com.cn'),
array('conferenceId'=>232323,'conferenceTitle'=>'quanshi uuu','smeAccount'=>'http://www.dbjr.com.cn'),
array('conferenceId'=>8989,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.dbjr.com.cn'),
array('conferenceId'=>1234343212,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.dbjr.com.cn')
)
)
);
轉(zhuǎn)化為:
<rtxAccount>aaron</rtxAccount>
<ipAddr>192.168.0.12</ipAddr>
<conferenceList>
<conference>
<conferenceId>1212</conferenceId>
<conferenceTitle>quanshi 444</conferenceTitle>
<smeAccount>http://www.dbjr.com.cn</smeAccount>
</conference>
<conference>
<conferenceId>454</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.dbjr.com.cn</smeAccount>
</conference>
<conference>
<conferenceId>6767</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.dbjr.com.cn</smeAccount>
</conference>
<conference>
<conferenceId>232323</conferenceId>
<conferenceTitle>quanshi uuu</conferenceTitle>
<smeAccount>http://www.dbjr.com.cn</smeAccount>
</conference>
<conference>
<conferenceId>8989</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.dbjr.com.cn</smeAccount>
</conference>
<conference>
<conferenceId>1234343212</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.dbjr.com.cn</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
/**
*
* 將簡(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;
}
相關(guān)文章
新浪微博OAuth認(rèn)證和儲(chǔ)存的主要過(guò)程詳解
本文給大家介紹的是參考Twitter的認(rèn)證過(guò)程實(shí)現(xiàn)的新浪微博OAuth認(rèn)證和儲(chǔ)存的主要過(guò)程詳解2015-03-03PHP strcmp()和strcasecmp()的區(qū)別實(shí)例
這篇文章主要介紹了PHP strcmp()和strcasecmp()的區(qū)別實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-11-11php導(dǎo)出csv數(shù)據(jù)在瀏覽器中輸出提供下載或保存到文件的示例
這篇文章主要介紹了php導(dǎo)出csv數(shù)據(jù)在瀏覽器中輸出提供下載或保存到文件的示例,需要的朋友可以參考下2014-04-04Laravel學(xué)習(xí)教程之request validation的編寫
這篇文章主要給大家介紹了關(guān)于Laravel學(xué)習(xí)教程之request validation編寫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10PHP實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)開發(fā)教程
這篇文章主要為大家分享了PHP實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)開發(fā)教程,開發(fā)者可以通過(guò)授權(quán)后獲取用戶的基本信息,感興趣的小伙伴們可以參考一下2016-01-01