Как бы распарсить XML данные в ассоциированый масив?
Может есть у кого готовое решение.
XML данные могут быть любые например многоуровневые
HTML |
<member_question></member_question> <member_answer></member_answer> <name></name> <showmenu> <item>flobmenu</item> <item>foomenu</item> <item>homenav</item> </showmenu> <lang> <language id="ru"> <name>ru</name> <prefix>__ru</prefix> <cataloge></cataloge> <title>Русский</title> <charset>windows-1251</charset> <default>1</default> <base></base> </language> <language id="en"> <name>en</name> <prefix>__en</prefix> <cataloge></cataloge> <title>English</title> <charset>UTF-8</charset> <default>0</default> <base></base> </language> </lang> |
или одноуровневый:
HTML |
<member_question></member_question> <member_answer></member_answer> <member_code_validation></member_code_validation> <member_ip>127.0.0.1</member_ip> <member_date_registered>1212963999</member_date_registered> <member_date_lastlogin>1238499874</member_date_lastlogin> <member_emailtime>1234972240</member_emailtime> <member_hash>0c04834578288556c76540bc32bd5e38</member_hash> |
С одноуровневым все просто но надо разные XML данные парсить в многоуровневый ассоциированый массив.
если через стандартный xml_parser или библиотека для PHP5 (не помню навание) то не всегда они имеются (у меня например на 5-ой xml_parser нет а на 4-ой ... )
Надеюсь на вашу помощь
Спустя 22 часа, 56 минут, 19 секунд (2.04.2009 - 12:30) Гость_oskin написал(а):
Спустя 15 минут, 36 секунд (2.04.2009 - 12:45) Гость_oskin написал(а):
разоррался. Если в xml кирилица то не обрабатывает
Спустя 1 час, 38 минут, 2 секунды (2.04.2009 - 14:23) Guest написал(а):
В принципе вот что получилось:
PHP |
/* * @ defined */ define( "XML_CHARSET", "Windows-1251" ); /* * * @ XML Block * * * * * * * * @ XML_get_aarray */ function XML_get_aarray($contents, $get_attributes = 1, $priority = 'tag') { if( int_Version() >= 5000 ) { $contents = iconv( XML_CHARSET, "UTF-8", $contents); } if (!function_exists('xml_parser_create')) { return array (); } $parser = xml_parser_create(); 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), $xml_values); xml_parser_free($parser); if (!$xml_values) return;
$xml_array = array (); $parents = array (); $opened_tags = array (); $arr = array (); $current = & $xml_array; $repeated_tag_index = array (); foreach ($xml_values as $data) { unset ($attributes, $value); extract($data); $result = ''; // array (); $attributes_data = array (); if (isset ($value)) { if ($priority == 'tag') $result = $value; else $result['value'] = $value; } if (isset ($attributes) and $get_attributes) { foreach ($attributes as $attr => $val) { if ($priority == 'tag') $attributes_data[$attr] = $val; else $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 ($attributes_data) $current[$tag . '_attr'] = $attributes_data; $repeated_tag_index[$tag . '_' . $level] = 1; $current = & $current[$tag]; } else { if (isset ($current[$tag][0])) { $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; $repeated_tag_index[$tag . '_' . $level]++; } else { $current[$tag] = array ( $current[$tag], $result ); $repeated_tag_index[$tag . '_' . $level] = 2; if (isset ($current[$tag . '_attr'])) { $current[$tag]['0_attr'] = $current[$tag . '_attr']; unset ($current[$tag . '_attr']); } } $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; $current = & $current[$tag][$last_item_index]; } } elseif ($type == "complete") { if (!isset($current[$tag])) { $current[$tag] = $result; $repeated_tag_index[$tag . '_' . $level] = 1; if ($priority == 'tag' and $attributes_data) $current[$tag . '_attr'] = $attributes_data; } else { if (isset ($current[$tag][0]) and is_array($current[$tag])) { $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; if ($priority == 'tag' and $get_attributes and $attributes_data) { $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; } $repeated_tag_index[$tag . '_' . $level]++; } else { $current[$tag] = array ( $current[$tag], $result ); $repeated_tag_index[$tag . '_' . $level] = 1; if ($priority == 'tag' and $get_attributes) { if (isset ($current[$tag . '_attr'])) { $current[$tag]['0_attr'] = $current[$tag . '_attr']; unset ($current[$tag . '_attr']); } if ($attributes_data) { $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; } } $repeated_tag_index[$tag . '_' . $level]++; } } } elseif ($type == 'close') { $current = & $parent[$level -1]; } } if( int_Version() >= 5000 ) { array_walk_recursive( $xml_array, 'XML__iconv'); } return ($xml_array); } /* * @ XML__iconv */ function XML__iconv( & $value, $key ) { $value = iconv( "UTF-8", XML_CHARSET, $value); } /* * @ End XML block */ |
Спустя 1 минута, 8 секунд (2.04.2009 - 14:24) Гость_oskin написал(а):
может у кого получше чем указаный мною выше