Topic 1. XML and Web Services

Introduction

Ordinary websites, written in HTML, are very much geared towards the end user. The end user uses the HTML interface to perform particular tasks. However the end user is not the only possible consumer of information on the web. What about other applications? A web service is an application running on a web server which provides raw information to other applications. These other applications are clients to the web service because they make a connection to it and process the information sent back. Here are a few examples of web services and their clients:

How is information sent back from web services?

Web services obviously send back information to their clients, but what format is typically used? HTML is one possibility but is not considered a good idea because HTML contains not only data, but also page structure information (headings, paragraphs, tables etc). A client to the web service might wish to arrange the information in a different way. Also some web service clients are not websites at all, for example the Java application used by the travel agent in the above example, or the weather "app". We could still use HTML, however it would be tricky to write a parser - a program which interprets the data and extracts the relevant information from it - as HTML contains all manner of formatting and style information in addition to the data itself.

So what we want is a format which represents the data, and the data alone. XML is one such format, and will be described here. Another is JSON: we will not have time to look at this today, but it is something which we will do later.

XML

XML is a data description format which, like HTML, uses tags. Unlike HTML, XML is not a language as such, but rather a format for describing data. With XML, we make up our own tags to accurately describe the data. For example, some data describing students might look as follows:




 Mark Gill 
9gillm69
07111 111111



 Steve Mills 
1mills63
07222 222222



 Rob Price 
5pricr67
07333 333333



]]>
Note how:

To be so-called well-formed XML, data must match these syntax rules:

  1. There should be exactly one (no more) top level tag (called a root tag), e.g. students in the above example.
  2. All opening tags must be closed.
  3. Tags must nest in the correct order, e.g. in the above example, because we open name after student , we must close name before we close student.
  4. XML is case sensitive. We can use lower or upper case for tags, but a particular tag must always be closed with a closing tag of the same case.

Why is XML so useful as a data format in web services?

As has already been indicated, XML is an ideal format for sending back data to clients from a web service. This is because it is pure data containing no presentation or formatting, meaning that the client application can parse (interpret) the XML and extract the data from it in whatever manner it sees fit. So we could have a web service which delivers weather data for a particular city as XML, and different clients could format it appropriately, for example a tourist board website could integrate the weather data using its own style, while a mobile phone "app" could do the same.

Exercises

Vital Information on Web Server and Database

You have an account for the Edward web server. If you were here last year, it's the same as last year. If you are a BIB student, please see me for your login details. Info about how to upload files using FTP is here.

You will also extensively be using the dftitutorials database. To access this, go to PHPMyAdmin and login with username dftitutorials/password dftitutorials. For today, you will be using the hits table within the dftitutorials database. For the assignment, you will use your own database with the same login as for FTP.

Exercise for this week

You are now going to start developing a web service. The web service is offered by HitTastic!, a leading online music store and pop chart database. PHP is the server-side language used in the unit; if you need any revision on PHP, please see the Developing for the Internet site here.

  1. Write a web service in PHP to look up all songs by a given artist, and return them to the client as XML. A database has been set up on the Edward server, containing a hits table (which stores the details on individual hits). The hits table contains the following columns: Write a PHP script which takes in an artist name from a query string, and returns all the songs by that artist as XML in the format given in the example above. If you have forgotten how to query a database in PHP, look at the example below for guidance, which searches for all songs by a given artist, supplied via a form, and generates the results in HTML. Note that you must begin your PHP script with:
    header("Content-type: text/xml");
    This line informs clients that XML is being sent back from the server, so they can act accordingly. Please use the dftitutorials login for the database, as given below.
     
    
    ";
    }
    mysql_close($conn);
    ?>
    
     ]]> 
    Hints: Once you have written it, upload it to your space on the Edward server and test it out by entering an appropriate URL in the web browser. The full list of songs in the database is available at:
    http://edward/ewt/hits.php
  2. (More advanced) Web services can also change information, as well as return it. HitTastic! wishes to provide a web service to reduce the quantity in stock of a song. This should take in a song ID and a desired quantity from a query string and reduce the quantity in stock accordingly. This script might look like this (you need to complete it!)
    
    ]]>    
    
    How might you indicate to the client whether the operation was successful? Remember that the client will be another application, not an end user.