Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function xml2array($contents, $get_attributes=1) {
- if(!$contents) return array();
- 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, $contents, $xml_values );
- xml_parser_free( $parser );
- if(!$xml_values) return; // Hmm...
- $xml_array = array();
- $parents = array();
- $opened_tags = array();
- $arr = array();
- $current = &$xml_array;
- foreach($xml_values as $data) {
- unset($attributes,$value);
- extract($data);
- $result = '';
- if($get_attributes) {//The second argument of the function decides this.
- $result = array();
- if(isset($value)) $result['value'] = $value;
- if(isset($attributes)) {
- foreach($attributes as $attr => $val) {
- if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
- }
- }
- } elseif(isset($value)) { $result = $value; }
- if($type == "open") {//The starting of the tag '<tag>'
- $parent[$level-1] = &$current;
- if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
- $current[$tag] = $result;
- $current = &$current[$tag];
- } else {
- if(isset($current[$tag][0])) { array_push($current[$tag], $result);
- } else { $current[$tag] = array($current[$tag],$result); }
- $last = count($current[$tag]) - 1;
- $current = &$current[$tag][$last];
- }
- } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
- if(!isset($current[$tag])) { //New Key
- $current[$tag] = $result;
- } else { //If taken, put all things inside a list(array)
- if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
- or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) { array_push($current[$tag],$result);
- } else { $current[$tag] = array($current[$tag],$result); }
- }
- } elseif($type == 'close') { $current = &$parent[$level-1]; }
- }
- return($xml_array);
- }
- $xml_tags = $all_xml_tags = array();
- $new_data_key = $data_num_key = false;
- $xml_data = xml2array($xml_contents,0);
- foreach($xml_data as $main_xml_value => $xml_general_data) {
- if (is_array($xml_general_data) && !empty($xml_general_data)) {
- foreach($xml_general_data as $xml_specific_key => $xml_specific_data) {
- if (is_array($xml_specific_data) && !empty($xml_specific_data)) {
- foreach($xml_specific_data as $data_num_key => $data_num_value) {
- if (is_array($data_num_value) && !empty($data_num_value)) {
- foreach($data_num_value as $new_data_key => $new_data_value) {
- if (is_array($new_data_value) && !empty($new_data_value)) {
- foreach($new_data_value as $new_data_xml_key => $new_data_xml_value) {
- if (!in_array($new_data_xml_key, $xml_tags) && !is_numeric($new_data_xml_key)) {
- $xml_tags[] = $new_data_xml_key;
- if (is_numeric($new_data_key)) {
- $new_data_key = $data_num_key;
- }
- $all_xml_tags[] = array('path' => $main_xml_value.'->'.$xml_specific_key.'->'.$new_data_key.'->'.$new_data_xml_key, 'xml_tag' => $new_data_xml_key);
- }
- }
- }
- else {
- if (!in_array($new_data_key, $xml_tags) && !is_numeric($new_data_key)) {
- $xml_tags[] = $new_data_key;
- $all_xml_tags[] = array('path' => $main_xml_value.'->'.$xml_specific_key.'->'.$new_data_key, 'xml_tag' => $new_data_key);
- }
- }
- }
- }
- }
- }
- }
- }
- else {
- echo '<h2 style="color:red">Invalid Feed.</h2>';
- }
Advertisement
Add Comment
Please, Sign In to add comment