1.5 Viewing comments and e4x
ECMAScript for XML ( E4X ) is a specification for amongst others Javascript and Actionscript, the programming language of Flex. E4X treats XML as a built in part of the language. Actionscript 3 has full support, however in the Javascript world E4X certainly is not supported in all browsers. Since XML has better support than JSON through E4X, I prefer using E4X.
Listing 7: Source of CommentWindows.mxml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?xml version="1.0" encoding="utf-8"?> <iic:ClosableTitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" initialize="srv.send();" title="Comments" xmlns:iic="net.ivanidris.flex.components.*"> <mx:Script> <![CDATA[ import mx.managers.PopUpManager; import mx.rpc.events.ResultEvent; import mx.collections.XMLListCollection; [Bindable] private var comment:XMLListCollection; private function resultHandler(event:ResultEvent):void { comment = new XMLListCollection (event.result..comment); } ]]> </mx:Script> <mx:HTTPService id="srv" url="/viewComments.py" result="resultHandler(event)" resultFormat="e4x"/> <mx:DataGrid x="10" y="0" dataProvider="{comment}" width="780" height="560"> <mx:columns> <mx:DataGridColumn headerText="Url" dataField="link"/> <mx:DataGridColumn headerText="Title" dataField="title"/> <mx:DataGridColumn headerText="Comments" dataField="comments"/> <mx:DataGridColumn headerText="Rating" dataField="rating" width="50"/> <mx:DataGridColumn headerText="User" dataField="user" width="70"/> </mx:columns> </mx:DataGrid> </iic:ClosableTitleWindow> |
A XMLListCollection stores the XML data, and serves as a data provider for a Flex DataGrid ( a special type of table ) and is for that reason tagged Bindable. Whenever the XMLListCollection changes the DataGrid is updated.viewComments.py is data provider to a HTTPService delivering comments data. It is important to note that the HTTPService has its resultFormat set to e4x. This produces a result object of type XML. Remember, that E4X treats XML as native objects, hence we can use special E4X expressions, such as event.result..comment. Every comment node is automagically converted and displayed as a row in the DataGrid. The magic occurs by matching the dataField attribute of each DataGridColumn to a child node of the aforementioned comment node.
Listing 8: Source of viewComments.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | from google.appengine.ext import db from net.ivanidris.flexreader import Feed comments = db.GqlQuery("SELECT * from EntryComment") print 'Content-Type: text/xml' print '' print '<items>' for comment in comments: print '<comment>' print ' <link>' + comment.link + '</link>' print ' <rating>' + str(comment.rating) + '</rating>' print ' <title>' + comment.title + '</title>' print ' <user>' + str(comment.user) + '</user>' print ' <comments><![CDATA[' + comment.comments + ' ]]></comments>' print '</comment>' print '</items>' |
viewComments.py prints all the comments in the datastore in XML format. Luckily, there are not that many comments there currently, otherwise I would have to do something about it. It doesn’t really matter, of course, in the worst case the Google servers will crash
.
More From ivanidris
ivanidris Recommends
- Seven Characteristics of Stepper Motors | Solder In The Veins (Solder In The Veins)
- 10 Questions with Facebook Research Engineer – Andrei Alexandrescu (Server-Side Magazine)

