Ajax: How DWR handles XML

Greg Murray recently noted that AJAX does not need to use XML (although he clearly likes the idea). He refers to DWR using text/plain in it's place. I thought it was worth noting how DWR handles XML.

DWR works a bit like RMI except that instead of having 2 Java systems talking to each other using JRMP you have a web browser and a web server talking using AJAX/HTTP.

DWR marshalls all the Java and Javascript parameters over an AJAX HTTP call, but it always uses text/plain even when it is transferring XML. How does it do this?.

Suppose you have a Java method that looks like this:

public org.w3c.dom.Document getNode() {
    /* some DOM code */
    return doc;
}

DWR generates Javascript for execution by the browser. For the example above the Javascript contains a text version of the XML document above. DWR arranges for it to be parsed using the browsers internal XML parser and then calls the appropriate callback to recieve the DOM tree.

This may seem like an overly complex way to do things but it means that you can pass around compound JavaBeans which contain a number of things including XML elements:

public class ReturnPojo {
    // Getters and setters omitted for brevity
    org.w3c.dom.Document doc;
    java.util.List collection;
    java.util.Properties settings;
}

We could pass a ReturnPojo to DWR and it would create a Javascript object to match in the client, including the DOM document parsed by the browsers parser. Such trickery would never be possible if we only used XML.

Comments

Comments have been turned off on old posts