The language is really growing up now though, and we have good implementations on the Java side such as Rhino.
Rhino even implements the latest and greatest of ECMAScript: ECMAScript for XML (E4X).
Now, XML is a first class citizen in the language which allows you to do some of the following:
Create a DOM from XML
var order = <order> <customer> <firstname>John</firstname> <lastname>Doe</lastname> </customer> <item> <description>Big Screen Television</description> <price>1299.99</price> <quantity>1</quantity> </item> </order>
Walk the XML tree a la XPath etc
// Construct the full customer name var name = order.customer.firstname + " " + order.customer.lastname; // Calculate the total price var total = order.item.price * order.item.quantity;
Construct a new XML object using expando and super-expando properties
var order = <order/>; order.customer.name = "Fred Jones"; order.customer.address.street = "123 Long Lang"; order.customer.address.city = "Underwood"; order.customer.address.state = "CA"; order.item = ""; order.item.description = "Small Rodents"; order.item.quantity = 10; order.item.price = 6.95;
Playing with SOAP
var message = <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Body> <m:GetLastTradePrice xmlns:m="http://mycompany.com/stocks"> <symbol>DIS</symbol> </m:GetLastTradePrice> </soap:Body> </soap:Envelope> // declare the SOAP and stocks namespaces var soap = new Namespace("http://schemas.xmlsoap.org/soap/envelope/"); var stock = new Namespace ("http://mycompany.com/stocks"); // extract the soap encoding style and body from the soap message var encodingStyle = [email protected]::encodingStyle; print("The encoding style of the soap message is specified by:\n" + encodingStyle); // change the stock symbol message.soap::Body.stock::GetLastTradePrice.symbol = "MYCO"; var body = message.soap::Body;
It is interesting to see ECMAScript leading the way in some areas. It is an interesting idea to have XML as such as first class citizen. I don’t remember having “tab delimited data” at the same level, and it is a little worrying to think about XML abuse that could occur because of it.
However, maybe it is time to take ECMAScript more seriously. It is installed in all browser VMs so to speak, and with implementations like Rhino, allows you to script Java in a simple way :)