Display number of items in xml file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Display number of items in xml file

Post by Tomcat7194 »

Hello, I'm new to PHP and I have a question.

I'm working with an xml file that shows flight delays (http://www.flightstats.com/go/rss/airportdelays.do).
In PHP, I need to make a variable that displays the total number of items in the flight delay xml file (i.e. the total number of current delays). I don't need to parse any of the information within the items--I just need code that will look at the XML file and make a variable that shows how many there are.

So for example, if there were two items in the XML file, the variable would equal 2, and I could display something like "Total Reported Delays=2".

Is there any simple way to do this? Will I need some kind of external file to parse the XML, or can PHP handle this kind of thing itself?

Thanks in advance,
Tom
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Just grab the xml file as a string, and use substr_count to count the instances of whatever tag you are using to hold a delay.
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Post by Tomcat7194 »

What should I use to load the XML as a string? My code right now keeps simply loading the XML url rather than the actual XML.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

file_get_contents() perhaps.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Post by Tomcat7194 »

My host is only running version 4.4, so no simple xml, but the file_get_contents worked perfectly. Thanks for the tip.
Tom
Post Reply