Here is two functions that i use to parse xml data.
PHP Code:
/**
* __get_xml_array($values, &$i)
*
* This is adds the contents of the return xml into the array for easier processing.
*
* @access private
* @param array $values this is the xml data in an array
* @param int $i this is the current location in the array
* @return Array
*/
function __get_xml_array($values, &$i)
{
$child = array();
if ($values[$i]['value']) array_push($child, $values[$i]['value']);
while (++$i < count($values))
{
switch ($values[$i]['type'])
{
case 'cdata':
array_push($child, $values[$i]['value']);
break;
case 'complete':
$name = $values[$i]['tag'];
$child[$name]= $values[$i]['value'];
if($values[$i]['attributes'])
{
$child[$name] = $values[$i]['attributes'];
}
break;
case 'open':
$name = $values[$i]['tag'];
$size = sizeof($child[$name]);
if($values[$i]['attributes'])
{
$child[$name][$size] = $values[$i]['attributes'];
$child[$name][$size] = $this->__get_xml_array($values, $i);
}
else
{
$child[$name][$size] = $this->__get_xml_array($values, $i);
}
break;
case 'close':
return $child;
break;
}
}
return $child;
}
/**
* _get_xml_array($data)
*
* This is adds the contents of the return xml into the array for easier processing.
*
* @access private
* @param string $data this is the string of the xml data
* @return Array
*/
function _get_xml_array($data)
{
$values = array();
$index = array();
$array = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $data, $values, $index);
xml_parser_free($parser);
$i = 0;
$name = $values[$i]['tag'];
$array[$name] = $values[$i]['attributes'];
$array[$name] = $this->__get_xml_array($values, $i);
return $array;
}
what you do is call _get_xml_array() and pass in the xml data and it will return an array for you with all of the xml data.
hope this helps..
Bookmarks