Reply to comment

XSLT for GWT

Here's a little lesson that I learned regarding client side XML and processing it. If your app requires any XML processing over about 50 nodes, use a native DOM implementation.

My app stores some Google Map locations in xml. Each location has the lat/long and an info node that I process in my app with XSLT to create the html for the marker window. Well, one file has 700+ of these location nodes, and when I wrapped AJAX-XSLT using the GWT, it took 2 full minutes to download the file, parse it, and process each location.

I switched to Sarissa, which is a JavaScript wrapper of the native DOM and XSLT support that most modern browsers have, and that same file now takes 4 seconds to do everything from download to processing. When I first got it working, I thought it was ignoring the xml file or something because it was so fast, but the Sarissa wrapper is doing it all, even remote DTD validation!

So, for those who need moderate to heavy use of XML, XSLT, and XPath for you apps, go with a native implementation like Sarissa. Don't have to redo all your work like I did.

My Implementation

If you would like to use my implementation, first go to http://sourceforge.net/projects/sarissa and download the latest release. You'll need to add these 3 files to your app:

  • sarissa.js
  • sarissa_dhtml.js
  • sarissa_ieemu_xpath.js

NOTE: Not all of these files may be included in future releases of Sarissa.

Then download the GWT wrapper at gwt.sarissa.zip. Note, this has no affiliation with the gwt.components guys, I just used their package path because it was convenient. Also, my apps only needs to read xml, so I didn't port any of the write xml methods.

Using the Code

After downloading the zip file, just extract it into your source directory for your GWT project and the compiler should pick it up without any problems.

I've gotten a lot of reports that this code isn't working. It might be that Sarissa has upgraded and the new version is not backwards compatible. Also, if you're creating the XML file, it will probably be faster to build your data in JSON as JavaScript can read it natively through its code syntax. However, I know some people are getting XML from a third-party source and it would be less efficient to transform it into JSON and then to parse the XML using the browser's native XML and XSLT functions. Plus there isn't, as far as I know, a transformation language for JSON like XSLT.

In any case, here's an example for using the API I provided:

/*
 * Notes
 * - I wrote this free hand, so I may have made syntax errors
 * - Normally you wouldn't want to put all the parsing and processing code in the RequestCallback
 */
RequestBuilder rb = new RequestBuilder( RequestBuilder.POST, "path/to/ xml/file/xmlfile.xml" );
rb.setHeader( "Content-Type", "text/xml; charset=utf-8" );
 
try {
	rb.sendRequest( "", new RequestCallback() {
 
		/*
		 * (non-Javadoc)
		 * @see com.google.gwt.http.client.RequestCallback#onResponseReceived(com.google.gwt.http.client.Request,
 		 * 	com.google.gwt.http.client.Response)
		 */
		public void onResponseReceived( Request request, Response response ) {
 
			if ( response.getStatusCode() != STATUS_CODE_OK ) {
				 // Capture error
				 return;
			}
 
			try {
				// Example of parsing the xml content into native DOM
				Node xmlDoc = XML.parse( response.getText() );
 
				// Example of finding a list of nodes
				List nodes = xmlDoc.getNodes( "/xpath/to/nodes" );
				for ( Iterator ni = nodes.iterator(); ni.hasNext(); ) {
					 Node n = (Node) ni.next();
					 System.out.println( n.getNodeName() + " = " + n.getStringValue() );
				}
 
				// Example of finding one node or the first noe
				Node node = xmlDoc.getNode( "/xpath/to/nodes" );
 
				// Example of applying a XSL file (assuming it was downloaded before hand)
				String transformedText = XSLT.process( xmlDoc, xsltDoc );
				System.out.println( transformedText );
			 }
			 catch ( XMLParseException xmlpe ) {
				// Handle exception
			 }
			 catch ( XSLTParseException xsltpe ) {
				// Handle exception
			 }
		 }
	 } );
}
catch ( RequestException re ) {
	 // Handle exception
}

 

AttachmentSize
gwt.sarissa.zip4.82 KB

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <strike> <caption>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>. The supported tag styles are: <foo>, [foo].

More information about formatting options