xml轉array

分享一個將xml文件轉成數組的方法。數組

 /** * @purpose: 將xml轉化爲數組 * @param mixed $url xml文件或者xml所在的url * @param int $getAttributes 是否獲取屬性值,若爲true,若是xml中有attributes,將attributes存入attr裏面 * @param string $priority 若是設置了$priority=tag,則標籤的值做爲數組中該標籤做爲key的value,不然會增長一級value的key * @return array|void */ public static function xml2array($url, $getAttributes = true, $priority = 'tag') { $contents = ''; //臨時保存xml內容 $arrXml = []; //xml轉數組後數據存放位置 if (!function_exists('xml_parser_create')) { goto __end; } $parser = xml_parser_create(''); if (!($fp = @ fopen($url, 'rb'))) { goto __end; } while (!feof($fp)) { $contents .= fread($fp, 8192); } fclose($fp); xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, trim($contents), $xmlValues); xml_parser_free($parser); if ($xmlValues) { $current = &$arrXml; $repeatedTagIndex = array(); foreach ($xmlValues as $data) { //確保每次循環的時候,沒有$attributes和$value這兩個變量 unset ($attributes, $value); //節點類型,open,close,complete $type = ArrayHelper::getValue($data, 'type'); //屬於幾級標籤,對應數組的幾維 $level = ArrayHelper::getValue($data, 'level'); //xml標籤名,做爲數組的key $tag = ArrayHelper::getValue($data, 'tag'); //每一個標籤裏面的數據 $value = ArrayHelper::getValue($data, 'value'); $result = array(); $attributesData = array(); if (isset ($value)) { if ($priority == 'tag'){ $result = $value; }else{ $result['value'] = $value; } } if (isset ($attributes) && $getAttributes) { foreach ($attributes as $attr => $val) { if ($priority == 'tag'){ $attributesData[$attr] = $val; }else{ //將全部的屬性以數組的形式保存在$result['attr']下面 $result['attr'][$attr] = $val; } } } //根據不一樣的節點類型所屬維度將數據變成一個數組 if ($type == "open") { $parent[$level - 1] = &$current; if (!is_array($current) or (!in_array($tag, array_keys($current)))) { $current[$tag] = $result; if ($attributesData){ $current[$tag . '_attr'] = $attributesData; } $repeatedTagIndex[$tag . '_' . $level] = 1; $current = &$current[$tag]; } else { if (isset ($current[$tag][0])) { $current[$tag][$repeatedTagIndex[$tag . '_' . $level]] = $result; $repeatedTagIndex[$tag . '_' . $level]++; } else { $current[$tag] = [ $current[$tag], $result, ]; $repeatedTagIndex[$tag . '_' . $level] = 2; if (isset ($current[$tag . '_attr'])) { $current[$tag]['0_attr'] = $current[$tag . '_attr']; unset ($current[$tag . '_attr']); } } $lastItemIndex = $repeatedTagIndex[$tag . '_' . $level] - 1; $current = &$current[$tag][$lastItemIndex]; } } elseif ($type == "complete") { if (!isset ($current[$tag])) { $current[$tag] = $result; $repeatedTagIndex[$tag . '_' . $level] = 1; if ($priority == 'tag' && $attributesData) $current[$tag . '_attr'] = $attributesData; } else { if (isset ($current[$tag][0]) && is_array($current[$tag])) { $current[$tag][$repeatedTagIndex[$tag . '_' . $level]] = $result; if ($priority == 'tag' && $getAttributes && $attributesData) { $current[$tag][$repeatedTagIndex[$tag . '_' . $level] . '_attr'] = $attributesData; } $repeatedTagIndex[$tag . '_' . $level]++; } else { $current[$tag] = [ $current[$tag], $result, ]; $repeatedTagIndex[$tag . '_' . $level] = 1; if ($priority == 'tag' && $getAttributes) { if (isset ($current[$tag . '_attr'])) { $current[$tag]['0_attr'] = $current[$tag . '_attr']; unset ($current[$tag . '_attr']); } if ($attributesData) { $current[$tag][$repeatedTagIndex[$tag . '_' . $level] . '_attr'] = $attributesData; } } $repeatedTagIndex[$tag . '_' . $level]++; //0 and 1 index is already taken  } } } elseif ($type == 'close') { $current = &$parent[$level - 1]; } } } __end: return $arrXml; }

上面的 這幾行,若是不是使用的yii框架,可用 extract($data)代替。框架

//節點類型,open,close,complete $type = ArrayHelper::getValue($data, 'type'); //屬於幾級標籤,對應數組的幾維 $level = ArrayHelper::getValue($data, 'level'); //xml標籤名,做爲數組的key $tag = ArrayHelper::getValue($data, 'tag'); //每一個標籤裏面的數據 $value = ArrayHelper::getValue($data, 'value');
相關文章
相關標籤/搜索