探討:array2xml和xml2array以及xml與array的互相轉(zhuǎn)化
php在做后臺(tái)服務(wù)器的時(shí)候,經(jīng)常會(huì)遇到這種情況,需要解析來自前臺(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.dhdzp.com'),
array('conferenceId'=>454,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.dhdzp.com'),
array('conferenceId'=>6767,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.dhdzp.com'),
array('conferenceId'=>232323,'conferenceTitle'=>'quanshi uuu','smeAccount'=>'http://www.dhdzp.com'),
array('conferenceId'=>8989,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.dhdzp.com'),
array('conferenceId'=>1234343212,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.dhdzp.com')
)
)
);
轉(zhuǎn)化為:
<rtxAccount>aaron</rtxAccount>
<ipAddr>192.168.0.12</ipAddr>
<conferenceList>
<conference>
<conferenceId>1212</conferenceId>
<conferenceTitle>quanshi 444</conferenceTitle>
<smeAccount>http://www.dhdzp.com</smeAccount>
</conference>
<conference>
<conferenceId>454</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.dhdzp.com</smeAccount>
</conference>
<conference>
<conferenceId>6767</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.dhdzp.com</smeAccount>
</conference>
<conference>
<conferenceId>232323</conferenceId>
<conferenceTitle>quanshi uuu</conferenceTitle>
<smeAccount>http://www.dhdzp.com</smeAccount>
</conference>
<conference>
<conferenceId>8989</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.dhdzp.com</smeAccount>
</conference>
<conference>
<conferenceId>1234343212</conferenceId>
<conferenceTitle>quanshi meetting</conferenceTitle>
<smeAccount>http://www.dhdzp.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
/**
*
* 將簡(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)文章
Yii2實(shí)現(xiàn)增刪改查后留在當(dāng)前頁(yè)的方法詳解
yii2.0框架是PHP開發(fā)的一個(gè)比較高效率的框架,集合了作者的大量心血,下面這篇文章主要介紹了Yii2如何實(shí)現(xiàn)增刪改查后仍留在當(dāng)前頁(yè)的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01
php+js實(shí)現(xiàn)異步圖片上傳實(shí)例分享
本來想用插件上傳圖片的,后來自己寫了一個(gè)簡(jiǎn)單的js實(shí)現(xiàn)異步的圖片上傳,不多說上代碼非常簡(jiǎn)單2014-06-06
laravel使用redis隊(duì)列實(shí)例講解
這篇文章主要介紹了laravel使用redis隊(duì)列實(shí)例講解,使用laravel框架之后配置redis還是很簡(jiǎn)單的,有感興趣的同學(xué)可以學(xué)習(xí)下2021-03-03
php加密算法之實(shí)現(xiàn)可逆加密算法和解密分享
對(duì)于大部分密碼加密,我們可以采用md5、sha1等方法??梢杂行Х乐箶?shù)據(jù)泄露,但是這些方法僅適用于無需還原的數(shù)據(jù)加密。對(duì)于需要還原的信息,則需要采用可逆的加密解密算法,下面一組PHP函數(shù)是實(shí)現(xiàn)此加密解密的方法2014-01-01

