Parsing XML from a returned 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
WNRosenberg
Forum Newbie
Posts: 2
Joined: Mon Jan 21, 2008 10:45 am
Location: New Haven, CT

Parsing XML from a returned file.

Post by WNRosenberg »

I'm currently working on a large-scale PHP & MySQL application for a shipping company. One of the features of the application is a function that will find the driving distance between two points, with "driving" being the key word. There's numerous utilities that will find the distance "as the crow flies" (a straight line), but the client needs to know driving distance.

I thought doing it with the Google Maps API, but since the application will only be visible for registered users (employees, customers, etc.), I'd have to go with the Enterprise edition, and I'm looking for a free solution. (I suppose I could just go with the regular API and hope Google never checks up on it but I'd rather avoid that issue.) I did find a way to get distance data directly from Google Maps without using the Maps API.

For example, if you are doing a search for the driving distance between New York City, NY (10007) and New Haven, CT (06511) you can plug in the two ZIP codes (or full addresses) into the following url:

http://maps.google.com/maps?q=from+1000 ... output=kml

which return a .kml file. The .kml extension is an XML format used with Google Earth. The distance that is returned with the file can be found in the following code block:

Code: Select all

<description>
  <![CDATA[Distance: 82&#160;mi (about 1 hour 54 mins)<br/>Map data &#169;2008 Tele Atlas ]]>
</description>
In older versions of this code, the tags were a bit different but the data contained in the CDATA (before the <br/> tag) was identical.

Ideally, I'd like to set up a search function where the user types in two addresses (or chooses them from the database) and then this script will calculate the driving distance by parsing through the file that is returned for the text "Distance: ([0-9\.,])&#160;([a-z\.])".

Does anyone have any ideas how to do this?

Thanks,
Will
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Parsing XML from a returned file.

Post by Christopher »

I don't think Google minds and the only difference is that the free account has a cap on the number of access per day -- but I recall the number is high.

Take a look at the SimpleXML library for PHP. It will parse the XML and give you an array to traverse. Use print_r() or similar to take a look at what is returned.
(#10850)
WNRosenberg
Forum Newbie
Posts: 2
Joined: Mon Jan 21, 2008 10:45 am
Location: New Haven, CT

Re: Parsing XML from a returned file.

Post by WNRosenberg »

arborint wrote:I don't think Google minds and the only difference is that the free account has a cap on the number of access per day -- but I recall the number is high.
I'm pretty sure the client will be well under the 15000 limit.
arborint wrote:Take a look at the SimpleXML library for PHP. It will parse the XML and give you an array to traverse. Use print_r() or similar to take a look at what is returned.
That would be an awesome tool to use if it didn't require PHP 5 -- the client is running PHP 4.
Post Reply