6.7 XML and E4X
The Actionscript Server Side Framework does not provide the same XML parsing tools provided in Flash, in particular the XML() object is missing, at least in the form that Flash developer know it since the days of Flash 5.
In chapter 8.7 we provide a tutorial showing how to use a Java-based parser, integrating it in the Actionscript code.
A more powerful way of handling XML data is provided by the ECMAScript for XML (E4X) language which extends the standard Actionscript / Javascript set of functionalities.
From SmartFoxServer version 1.5.0 we've added support to the E4X extensions, thanks to the Rhino javascript engine.
» A quick introduction to E4X
A lot of documentation already exist about E4X with good and detailed examples. Additionally E4X was also adopted by Adobe in the latest Actionscript 3.0 specifications, which will probably make it quite popular.
Here's a selection of articles that may help you learning more about E4X:
-> Adobe labs introduction
-> Brajeshwar's article on E4X
-> W3Schools introduction to E4X
-> Ecmascript E4X language specifications
The most interesting aspect of E4X is that it makes XML a native datatype, allowing developers to traverse the XML tree easily and efficiently.
In this article we will reuse the same XML example file from tutorial 8.7 and we'll use E4X to parse it. You will notice how simple and concise is the resulting code:
// Global extension variables var xmlObj = null function init() { // Read xml file from disk var xmlData = _server.readFile("sfsExtensions/data/books.xml") // Create an XML object xmlObj = new XML(xmlData) // Show data readTheXmlFile() } /* * Read and parse the XML file */ function readTheXmlFile() { // Show theand nodes trace("Collection Name: " + xmlObj.collectionName) trace("Collection Owner: " + xmlObj.collectionOwner + newline) var bookList = xmlObj.bookList.book for (var i = 0; i < bookList.length(); i++) { trace("Title : " + bookList[i].@title) trace("Author : " + bookList[i].@author) trace("Year : " + bookList[i].@year) trace("Publisher: " + bookList[i].@publisher) trace("-------------------------------------") } }
If you are familiar with object-based languages like Javascript and Actionscript, you will notice that the code is very simple to understand. The XML object behaves exactly like any other generic object with the peculiarity that nodes are represented as object properties, and attributes are identified by a "@" prefix.
The power of E4X is not just in the ability to traverse the XML tree as an object, but also to provide conditions for filtering nodes and attributes. The following example searches all books in our sample XML that were published in 2003.
// Global extension variables var xmlObj = null function init() { // Read xml file from disk var xmlData = _server.readFile("sfsExtensions/data/books.xml") // Create an XML object xmlObj = new XML(xmlData) // Example #1: search all books published in 2003 var books = xmlObj.bookList.book.(@year == 2003) for (var i = 0; i < books.length(); i++) { trace("Title : " + books[i].@title) trace("Author : " + books[i].@author) trace("Year : " + books[i].@year) trace("Publisher: " + books[i].@publisher) trace("-------------------------------------") } }
All the "magic" is performed in one single line of code:
var books = xmlObj.bookList.book.(@year == 2003)
Here follows one more example showing how to retrieve all books that contain the keyword "Java" in their title attribute:
// Global extension variables var xmlObj = null function init() { // Read xml file from disk var xmlData = _server.readFile("sfsExtensions/data/books.xml") // Create an XML object xmlObj = new XML(xmlData) // Example #2: match a substring in name attribute var books = xmlObj.bookList.book.(@title.toString().indexOf("Java") > -1) for (var i = 0; i < books.length(); i++) { trace("Title : " + books[i].@title) trace("Author : " + books[i].@author) trace("Year : " + books[i].@year) trace("Publisher: " + books[i].@publisher) trace("-------------------------------------") } }
Not only E4X is great at quickly parsing complex XML data but it's also extremely handy in dynamically populating new XML data.
In order to learn more we highly recommend to check the articles presented at the beginnig of this article.
doc index |