Retrieve Values from XML into PHP array

I've written a small function to read an XML file with simpleXML and used Pear Benchmark Iterate to time it. I've read that simpleXML and DOM puts the entire XML file into memory which can create a lot of overhead on files of a large size.

The function below is a relatively small size and I'm only pulling 10 values from the XML file.

Iterations of the function below (xml filesize: 200kb) takes 2.5 seconds to complete on average.

Can anyone suggest a solution with PHP XMLparser, a related class, or perhaps some other efficient way to parse xml into an array?

SimpleXML version

function getItems($file_id, $item_count=5)
{
    switch ($file_id)
    {
        case '1':
        $file = "http://xml_file.xml";

        if ($xml = simplexml_load_file($file)) 
        {
            $i=0;
            foreach ($xml->info as $info) 
            {
                if ($i < $item_count)
                {
                    $var[] = array(
                        "id"  =>  (string)$info->id,
                        "name" => (string)$info->name); 
                }
                $i++;   
            }
            return $var;
        }
    }
}

Answers


The XML Parser module may be the better option that doesn't require some external library, although it requires a bit of a mind shift. They payback is that the data is never loaded into memory in its entirety.

In a nutshell, you will need to treat the XML document in a tree descending matter and keep a journal of your location in the tree as opening and closing tags are reported to you through handlers.

It may seem daunting that this module is not OO, but xml_set_object alleviates this.


Need Your Help

Removing Administrator Privilages from Process

winapi uac access-token elevated-privileges privilege-elevation

With the help of this great MSDN article, my first idea was to simply check if the process is using an elevated Administrator group, and using AdjustTokenGroups() I would set the Administrator grou...

asp.net SERVER_NAME returns wrong domain name

c# asp.net iis

Can anyone explain me how the asp.net/iis gets SERVER_NAME variable ?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.