Forum Moderators: coopster

Message Too Old, No Replies

PHP SimpleXML - get node instead of whole file

         

cantona

4:29 pm on Mar 24, 2009 (gmt 0)

10+ Year Member



Hi

I want to use bits of data from external XML files and save them into a PHP array.

Currently I'm doing this as so...


$xml = simplexml_load_string(file_get_contents($xml_file_url));

But I'm getting some serious load time issues. The files are just too big. I only need a few bits of data from each file, not the whole thing. Hopefully this will speed up the page load time.

Is there a way of specifying which tags/nodes you want to use?

Thanks

idev

6:47 am on Mar 27, 2009 (gmt 0)

10+ Year Member



you can definitely do that by reading the file directly, accessing the file, and searching for the desired node. As I can see from SImpleXML API, it doesn't support that.

eeek

12:35 am on Mar 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



accessing the file, and searching for the desired node

And I shudder to think what kind of bugs that could create.

idev

7:34 pm on Mar 28, 2009 (gmt 0)

10+ Year Member



It depends on the development skill of cantona.

BradleyT

6:13 pm on Mar 30, 2009 (gmt 0)

10+ Year Member



I do the read locally for speed thing. I created a page that saves a local copy of the XML file. I then set up a cron to run this page each night at midnight.

To save file locally


$file = "http://example.com/feedurl";
$ch = curl_init($file);
$fp = @fopen("formvalues.xml", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Then inside my form page I use


$filename = "formvalues.xml";
if (file_exists($filename)) {
$xml = simplexml_load_file($filename);
$titles = $xml->xpath("//select[@name='title']//option[@value]");
foreach($titles as $key => $value)
{
$titleddl[(string)$value['value']] = (string)$value;
}
...
}

coopster

3:04 pm on Mar 31, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Good point, BradleyT -- I was going to mention using xpath. The only warning I have is ... and I'm digging deep into memory here ... I believe once I came across an issue because the XML namespace was malformed or something along those lines.

WOW. I just did a quick search across the forums wondering if it was helping somebody else out here and got a hit. Details:

[webmasterworld.com...]

Little_G

3:15 pm on Mar 31, 2009 (gmt 0)

10+ Year Member



Hi,

Using XMLReader [php.net] might be more appropriate for you, it reads one node at a time from the file without loading the whole thing into memory.

Andrew