Monday, August 10, 2009

Ajax: Xpath-enabled Json queries

In the Ajax world, there is a big discussion about XML versus Jsonas a means of transporting data from the Server to the Browser. After looking at the first examples of using a DOM API in Javascript, I knew, I'll not be using XML in Javascript. Using Json instead, feels very nice and natural. But Json is just a means of transport. Nothing else. The simplisticclasses work, but no query language to select the interesting data before sending it over. If you structure your Web-Gui well, the resulting data should be simple. Hence no need for the complex XML structure. XML offers however, two goodies, I wouldn't want to miss: Object to XML serializers (e.g. XStream) and XPath to select parts of the XML. So my idea is this: why not marry both approaches ? Transform the Java object into XML, apply an XPath query to select what you really want and then transform the result to Json to send it over. Even more flexible, support a 'class' and 'method' (and an optional 'args') parameter to select which data to get. Here is how it works: 1) Ajax Request: On the Browser, request a Json object with parameters class,method, xpath:
requestList( "class=TibjmsAdmin&method=getTopics&xpath=/*/*/name",
"name",
isAsync,
topicList );
2) Java Method call: On the Server-side, this request will execute the method TibjmsAdmin.getTopics() and create a Java object with some code similar to this:

if( className.equals("TibjmsAdmin") ) { // TODO: use ClassLoader (security?)
 Object invokee = jmsAdmin;
}
... others ...

// get specified method on specified class
className = invokee.getClass().getName();
Class c = Class.forName(className); // className is TibjmsAdmin
Method m = c.getMethod(method,argClasses); // method is getTopics, argClasses is null

// invoke method and get return object
  returnObj = m.invoke(invokee, argObs);
3) Java to XML: the next step is now to serialize this Java object to XML. I like to use XStream for this purpose because its extremly simple and straight-forward.

XStream xstream = new XStream();
String xml = xstream.toXML(returnObj);
If the returned object does not produce the required results, you can do some magic by plugging in an XStream 'Converter' (not shown here). 4) Apply Xpath: Now that that we have the XML, we can apply the specified XPath to fiddle out the data that is really interesting for us. The following code will create a list of selected XML nodes.
    NodeList nl = XmlUtils.selectNodeList(docBuilder,xmlString,xpath);
StringBuffer sb = new StringBuffer("");
for( int i=0;i<nl.getLength();i++ ) {

  Node node = nl.item(i);
  sb.append("<" + node.getNodeName() + ">" );
  sb.append( node.getTextContent() );
  sb.append("" );
}
sb.append("\n");
5) Return Json result: finally return the Json object back to the Browser using the Json tools.
JSONObject json = XML.toJSONObject(xml);
  response.setContentType("text/plain");
if( out.equals("pretty") ) // pretty JSON
  response.getWriter().print( json.toString(2) );
else // or not
  response.getWriter().print( json.toString() );

No comments:

Post a Comment