I've been poking around Drupal's XML-handling functions and don't see anything that provides simple XML-to-array conversion. To parse XML, it looks like you have to create your own parser, which means writing several functions for tag handling, etc. It would be nice to be able to do something like the following, which uses Technorati's REST API retrieve a list of blogs that link to a URL:
$request = "http://api.technorati.com/cosmos?key=$mykey&url=$url";
$result = drupal_http_request($request);
$vals = xml2array( $result->data );
$items = $vals['tapi'][0]['document'][0]['item'];
foreach ($items as $item) {
if ($item['nearestpermalink']) {
$links[] = l($item['weblog'][0]['name'], $item['nearestpermalink']);
}
Here's the xml2array function that would do the heavy lifting:
function xml2array( $textXml )
{
$regExElements = '/<(\w+)([^>]*)>(.*?)<\/\\1>/s';
$regExAttributes = '/(\w+)="([^"]*)"/';
preg_match_all( $regExElements, $textXml, $matchElements );
foreach ( $matchElements[1] as $keyElements=>$valElements ) {
if ( $matchElements[2][$keyElements] )
{
preg_match_all( $regExAttributes, $matchElements[2][$keyElements], $matchAttributes );
foreach ( $matchAttributes[0] as $keyAttributes=>$valAttributes )
{
$arrayAttributes[ $valElements.' attributes' ][$matchAttributes[1][ $keyAttributes ] ] = $matchAttributes[2][ $keyAttributes ];