Data Transformations

Once you’ve retrieved your data from a web service, you need to turn that XML into something fit for human consumption. This means either translating it into HTML or dynamically building Document Object Model (DOM) structures in JavaScript.

But the choices aren’t quite so clear cut. What if your web service query returns more information than the browser can practically parse? What if you don’t want to expose certain XML elements to the browser? (If the browser can see the XML, then prying eyes can as well.) What if you want to minimize data parsing in the browser?

You have a choice to transform your XML data within the browser or on the server, or some combination of the two. Each transformation pattern has its own set of trade-offs. Let’s explore some of the possibilities.

A Design Pattern: Intelligent Proxy Server and Minimal Browser Processing

In this pattern, an intelligent proxy server acts as intermediary between the browser and the web service. The server’s proxy script has information about how to transform a specific web service request into data the browser can easily use. Even within this pattern, there are additional trade-offs to consider. The server can send a snippet of HTML back to the browser, which can then insert it into the page using a DOM innerHTML method. Or the server can send the browser JSON or simple structured text data, which the browser can easily parse to build DOM objects that update the web page. There are even more possibilities, ...

Get Ajax and Web Services now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.