XML to Array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Guys,
I am hoping someone can guide me or help me fix this issue.
I have been lost in the code for some time now.
I am trying to get the attributes of an xml node.
I have this code:

function xml_parse_into_assoc($data)
{
    $p = xml_parser_create();
		  if(stripos($data, "http", 0) !== false) {
		    if (!($fp = @ fopen($data, 'rb')))
		    {
		        return array ();
		    }
		    while (!feof($fp))
		    {
		        $xml .= fread($fp, 8192);
		    }
		    fclose($fp);
	    } else if(stripos($data, "<?", 0) !== false) {
	    	$xml .= $data;
	    }
		  xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
		  //xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
		  xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
		
		  xml_parse_into_struct($p, $xml, $vals, $index);
		  xml_parser_free($p);
		
		  $levels = array(null);
		
		  foreach ($vals as $val) {
		    if ($val['type'] == 'open' || $val['type'] == 'complete') {
		      if (!array_key_exists($val['level'], $levels)) {
		        $levels[$val['level']] = array();
		      }
		    }
		
		    $prevLevel = &$levels[$val['level'] - 1];
		    $parent = $prevLevel[sizeof($prevLevel)-1];
		
		    if ($val['type'] == 'open') {
		      $val['children'] = array();
		      $val['attributes'] = array();
		      array_push(&$levels[$val['level']], $val);
		      continue;
		    }
		
		    else if ($val['type'] == 'complete') {
		      $parent['children'][$val['tag']] = $val['value'];
		    }
		
		    else if ($val['type'] == 'close') {
		      $pop = array_pop($levels[$val['level']]);
		      $tag = $pop['tag'];
		
		      if ($parent) {
		        if (!array_key_exists($tag, $parent['children'])) {
		          $parent['children'][$tag] = $pop['children'];
		        }
		        else if (is_array($parent['children'][$tag])) {
		            if(!isset($parent['children'][$tag][0])) {
		                $oldSingle = $parent['children'][$tag];
		                $parent['children'][$tag] = null;
		                $parent['children'][$tag][] = $oldSingle;
		
		            }
		              $parent['children'][$tag][] = $pop['children'];
		        } else if (array_key_exists('attributes', $val)) {
		            if (isset($val['value'])) {
		                $parent['children'][$tag] = $val['value'];
		            }
		            foreach ($val['attributes'] as $key=>$value) {
		                $parent['children'][$tag][$key] = $value;
		            }
		      	}
		      } else {
		        return(array($pop['tag'] => $pop['children']));
		      }
		    }
		
		    $prevLevel[sizeof($prevLevel)-1] = $parent;
		  }
}

This is the part I am adding to try and acheive this.

...
 else if (array_key_exists('attributes', $val)) {
		            if (isset($val['value'])) {
		                $parent['children'][$tag] = $val['value'];
		            }
		            foreach ($val['attributes'] as $key=>$value) {
		                $parent['children'][$tag][$key] = $value;
		            }
		      	}
...

I do admit I haven't the foggiest idea what I am doing here, so I am noobing out here on how this php function is really working. I got it off php.net and am trying to implement it in my code. Without my addition it works well except it doesn't grab any attributes
which I need in order for my script to work properly.

Any ideas on what I am doing wrong?

TIA,

Best,
Karl

PS: please be gentle.. (: ))


Karl DeSaulniers
Design Drumm
http://designdrumm.com


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux