PHP - XML Exercise

SimpleXML

Since PHP 5.0, a very simple way of accessing and updating XML has been available - SimpleXML. This set of functions allows XML strings and files to be converted to objects so that nodes (elements) in the tree can be accessed just with object references. In PHP the equivalent of the . in Java is -> so $x->y accesses the y child of $x. Attributes are accessed as an associative array $x['p1']. This provides a much more natural way of navigating in a tree structure than that provided by the W3C DOM interface based on childNode() etc.

Weather.com

To make this exercise more dynamic, we will use The Weather Channel at www.weather.com. This site provides live weather date for places around the world, but it also provides an API so that the data can be read into a server script for further processing. The interface uses a URL-line to send parameters to the service and the return is an XML document. To access the service, you must apply for a partner identifier and key, which enables the Weather Channel to track usage made by the partners. This key data should not be revealed to outsiders.

The guide sets out the terms and conditions for use of this data, which include links back to the Weather Channel site. These would have to be followed in any commercial use of the data but I am assuming that for educational purposes, this requirement is fullfilled by the general links on this page. There are also a number of constraints on refresh rates which require caching of data to reduce the hit rate. The code provide here is for teaching purposes only and would require a lot more work to create a commercial-strength application.

Weather from a file

Task 1

Weather from a remote source

Task 2