Bluish Coder

Programming Languages, Martials Arts and Computers. The Weblog of Chris Double.


2006-06-07

E4X with Server Side Javascript

I've updated the Server Side Javascript distribution to include an example which uses E4X.

E4X is described in Ecma-357 and adds a Native XML syntax to Javascript. The XML is converted to a Javascript object which can be used to query and manipulate the XML.

The E4X example servlet generates the XHTML to be displayed using:

var result = 
      <html>
        <head><title>HTML Using E4X!</title></head>
        <body>
          <p>Hello from E4X on: {new Date()}.</p>
        </body>
      </html>;

It embeds the current date and time by calling out to Javascript's Date object. This is done by wrapping the Javascript expression inside curly bracket: {new Date()}.

Given the XML object now stored in the 'result' variable it can be sent over the wire in the same manner as the other servlet examples:

var text = doctype + result.toString();
resp.setContentType("text/html")
resp.setContentLength(text.length)
resp.getOutputStream().print(text)
resp.flushBuffer()

I prepend an XHTML doctype to the output as the XML generated is actually XML not HTML. This means things like <br> need to be <br/>, etc. I've not found a way to get E4X to generate HTML which is a pity - if anyone knows if it is possible I'd appreciate you dropping a comment here.

The updated code is available as javascript-server-0.2.tar.gz and in the darcs repository:

darcs get http://www.bluishcoder.co.nz/repos/javascript-server

Tags


This site is accessable over tor as hidden service mh7mkfvezts5j6yu.onion, or Freenet using key:
USK@1ORdIvjL2H1bZblJcP8hu2LjjKtVB-rVzp8mLty~5N4,8hL85otZBbq0geDsSKkBK4sKESL2SrNVecFZz9NxGVQ,AQACAAE/bluishcoder/-44/


Tags

Archives
Links