php實現(xiàn)xml數(shù)據(jù)轉(zhuǎn)數(shù)組兩種方式
方法一、可以使用simplexml_load_string()函數(shù)將XML數(shù)據(jù)轉(zhuǎn)換為數(shù)組
$xmlData = '<root><name>John Doe</name><age>30</age></root>'; $xml = simplexml_load_string($xmlData); $json = json_encode($xml); $array = json_decode($json, true); print_r($array);

首先將XML數(shù)據(jù)存儲在一個字符串中,然后使用simplexml_load_string()函數(shù)將其轉(zhuǎn)換為SimpleXMLElement對象。
接下來,使用json_encode()函數(shù)將SimpleXMLElement對象轉(zhuǎn)換為JSON字符串,
再使用json_decode()函數(shù)將JSON字符串轉(zhuǎn)換為關(guān)聯(lián)數(shù)組。最后,使用print_r()函數(shù)打印出數(shù)組的內(nèi)容
方法二、使用PHP內(nèi)置的DOMDocument類來將XML數(shù)據(jù)轉(zhuǎn)換為數(shù)組的方法
$xmlData = '<root><name>John Doe</name><age>30</age></root>';
$xml = new DOMDocument();
$xml->loadXML($xmlData);
$array = domToArray($xml->documentElement);
print_r($array);
function domToArray($node) {
$array = array();
if ($node->hasAttributes()) {
foreach ($node->attributes as $attr) {
$array[$attr->nodeName] = $attr->nodeValue;
}
}
if ($node->hasChildNodes()) {
foreach ($node->childNodes as $childNode) {
if ($childNode->nodeType == XML_ELEMENT_NODE) {
$array[$childNode->nodeName] = domToArray($childNode);
} else if ($childNode->nodeType == XML_TEXT_NODE) {
$array['value'] = $childNode->nodeValue;
}
}
}
return $array;
}
這個例子創(chuàng)建了一個DOMDocument對象,并使用loadXML()方法將XML數(shù)據(jù)加載到該對象中。
然后,使用domToArray()函數(shù)將DOM文檔轉(zhuǎn)換為數(shù)組。
這個函數(shù)遞歸地遍歷XML節(jié)點,并將節(jié)點的名稱和值存儲在數(shù)組中。
如果節(jié)點有屬性,則也將其作為鍵值對存儲在數(shù)組中。
最后,使用print_r()函數(shù)打印出數(shù)組的內(nèi)容。
需要注意的是,domToArray()函數(shù)是遞歸的,它能夠處理復(fù)雜的XML結(jié)構(gòu)。請根據(jù)你的XML數(shù)據(jù)進行適當(dāng)?shù)恼{(diào)整
方法補充
方法一:
function xmlToArray(string $xml, $oldStr = [], $newStr = [])
{
//過濾特殊xml 格式
if (!empty($oldStr)) {
$xml = str_replace($oldStr, $newStr, $xml);
}
$objectXml = simplexml_load_string($xml);//將文件轉(zhuǎn)換成對象
$xmlJson = json_encode($objectXml);//將對象轉(zhuǎn)換個JSON
$xmlArray = json_decode($xmlJson, true);//將json轉(zhuǎn)換成數(shù)組
return $xmlArray;
}
方法二:數(shù)組和XML相互轉(zhuǎn)換
//數(shù)組轉(zhuǎn)xml
function ArrToXml($arr)
{
if(!is_array($arr) || count($arr) == 0) return '';
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
//Xml轉(zhuǎn)數(shù)組
function XmlToArr($xml)
{
if($xml == '') return '';
libxml_disable_entity_loader(true);
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $arr;
}方法三:
//獲取xml xml轉(zhuǎn)數(shù)組
private function xmlToarr($xml)
{
if (!$xml) {
throw new WxPayException("xml數(shù)據(jù)異常!");
}
//將XML轉(zhuǎn)為array
//禁止引用外部xml實體
libxml_disable_entity_loader(true);
$data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $data;
}
到此這篇關(guān)于php實現(xiàn)xml數(shù)據(jù)轉(zhuǎn)數(shù)組兩種方式的文章就介紹到這了,更多相關(guān)php xml轉(zhuǎn)數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php的chr和ord函數(shù)實現(xiàn)字符加減乘除運算實現(xiàn)代碼
這兩個函數(shù)到底有什么用呢? 用來做字符加減運算最合適了. 普通的字符是無法做加減運算指向下一個字符的. 而轉(zhuǎn)成ASCII后就可以做加減乘除了. 處理好后再轉(zhuǎn)成字符就可以了. 目前的很多字符串加密,解密都用到此功能!2011-12-12
php獲取遠程圖片的兩種 CURL方式和sockets方式獲取遠程圖片
php獲取遠程圖片的兩種:CURL方式和sockets方式獲取遠程圖片,需要的朋友可以參考下。2011-11-11
解決ThinkPHP關(guān)閉調(diào)試模式時報錯的問題匯總
本文給大家匯總了一下個人在項目中所遇到的關(guān)閉ThinkPHP的調(diào)試模式時遇到報錯的幾種情況以及最終的處理辦法,非常的簡單實用,這里分享給大家,有需要的小伙伴可以參考下。2015-04-04
如何使用PHP實現(xiàn)javascript的escape和unescape函數(shù)
本篇文章是對使用PHP實現(xiàn)javascript的escape和unescape函數(shù)的方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06

