Today we will combine the topics of the first two weeks, by examining how we can connect to a web service from an AJAX-based front end. We will connect to a web service from AJAX and then parse or interpret the XML returned. XML parsing can be made easier through the use of frameworks such as jQuery (which we will look at next week) but this week, we will examine the standard DOM method of XML parsing as it is an important concept to be aware of.
So far, we've seen the innerHTML property which can be used to read, or change, the text within an HTML element. However that's just the start: the DOM offers a whole range of ways to read and manipulate HTML pages or XML data. To understand how you can use DOM for document manipulation, you must understand the concept of nodes, which we will discuss below. The examples below use the DOM to manipulate HTML documents, however, more generally, the DOM is used for accessing and manipulating XML documents. An HTML web page is a particular, specific type of XML document. So, as well as using the DOM to query and manipulate web pages, we can use it in a more general sense to query and manipulate XML. In AJAX, this latter use of the DOM is used extensively.
<body> <p> Welcome to the <em>wonderful!</em> world of dynamic text!</p> </body>
<body> <p> Welcome to the <em>wonderful!</em> world of dynamic text!</p> </body>
parentElement.removeChild(childElement)
(Source: Quirksmode, a very useful reference site for JavaScript and the DOM)
for (var count=0; count<parent.childNodes.length; count++)
{
alert(parent.childNodes[count].nodeName); // show name of the node
}
// create a child node of the parent, assume that 'parent' has no children yet
var p = document.createElement("P");
parent.appendChild(p);
// create a text node
var textNode = document.createTextNode("some text");
// append it to the first child of the node 'parent', which will be the
// paragraph we created above. In other words 'p' and 'parent.firstChild'
// are the same in this case.
parent.firstChild.appendChild(textNode);
var p = document.createElement("P");
parent.appendChild(p);
the parentNode of p would be parent.
parentNode.insertBefore(newNode,parentNode.childNodes[3]);This code would insert newNode before child node 3 of the parent.
The first AJAX example from last time communicates with a server side script which sends back plain text. However, more frequently, the server side component of an AJAX application will send back XML data. In other words, the server side component of an AJAX application is often a web service. The callback function will then parse (interpret) the XML, using the DOM, and integrate the data within the website s front end. Using XML allows us to structure the data, meaning that we can be very flexible in how we present it to the end user we can extract the data we need from the XML (which may not necessarily be all of it) and arrange it on the page how we like.
This process of extracting the relevant data is called parsing XML. As you will see below, we use the DOM node manipulation functions, discussed above, to parse the XML.
The DOM feature which is most useful to us in doing this is getElementsByTagName() , which takes a particular tag name and returns an array of all tags with that name. For example imagine the server sent back the results of search for flights to Paris on 1/7/08 as this XML:
<flights> <flight> <destination>Paris</destination> <number>1</number> <time>0900</time> <date>1/7/08</date> </flight> <flight> <destination>Paris</destination> <number>3</number> <time>1400</time> <date>1/7/08</date> </flight> </flights>The example below would parse XML in the above format in the callback:
Mega Airways! - Europe's top value airline!
SPECIAL IBIZA OFFER!!!!!!!!!!!!!!!!!!!!
Fly all the way to Ibiza for only GBP5!!!!!!!
Excludes baggage fee of GBP100 per bag, weight limit 1kg, increasing by GBP50 per kg. Quoted price only valid for the 4am flight from Biggin Hill Airport on January 17th in leap-years ending with an 8. Normal price GBP500, excluding baggage fees as quoted above. The Ibiza airport we fly into is Ibiza(Valencia) Airport, mainland Spain, only a few hours away by boat. Equipment: early 1950s Boeing. Toilet fee of GBP10 on board.
Destination:
Date: