xml parsing

By sean on Sep 14, 2006

Just fixed up a old xml function I had laying around. This will return an associative array. I dug it up for a news bot Im coding, it reads google rss feeds.

<?php
function xml2array($elements_string)
{
    $xml_array = array();
    $elements_regex = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*?)<(\/\s*\1\s*)>)/s';
    $attributes_regex = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
    preg_match_all ($elements_regex, $elements_string, $elements_array);
    foreach ( $elements_array[1] as $e_key => $e_value )
    {
        $xml_array[$e_key]["name"] = $e_value;
        if ( ($attributes_string = trim($elements_array[2][$e_key])) )
        {
            preg_match_all($attributes_regex, $attributes_string, $attributes_array);
            foreach ( $attributes_array[1] as $a_key => $a_value )
            {
                $xml_array[$a_key]["attributes"][$a_value] = $attributes_array[2][$a_key];
            }
        }
        if ( ($p = strpos($elements_array[3][$e_key], "<")) > 0 )
        {
            $xml_array[$e_key]["text"] = substr($elements_array[3][$e_key], 0, $p - 1);
        }
        if ( preg_match($elements_regex, $elements_array[3][$e_key]) )
        {       
            $xml_array[$e_key]["elements"] = xml2array($elements_array[3][$e_key]);
        }
        else if ( isset($elements_array[3][$e_key]) )
        {
            $xml_array[$e_key]["text"] = $elements_array[3][$e_key];
        }
        $xml_array[$e_key]["closetag"] = $elements_array[4][$e_key];
    }
    return $xml_array;
}
$xml = xml2array(file_get_contents("./news.xml"));
print_r($xml);
?>

Comments

Sign in to comment.
tye   -  Jan 01, 2007

SPL contains a class for parsing XML, http://www.php.net/~helly/php/ext/spl/classSimpleXMLIterator.html
Although poorly documented so far it looks promising.

 Respond  
Sasuke   -  Sep 16, 2006

Yup!

 Respond  
Hawkee   -  Sep 15, 2006

XML snippets are always useful.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.