[SmartFox Pro] Using Java classes in Actionscript
[ July 29, 2005 ] by Marco Lapi, a.k.a Lapo
Article 7: learn how to obtain an unlimited amount of possibilities accessing the entire Java2 Framework and any other Java classes using the SmartFoxServer PRO Server Side Framework


The Server Side Framework in SmartFoxServer PRO is not only limited to the provided API, but it can easily access the entire Java2 Framework and any other Java classes, providing an unlimited amount of possibilities for developers. In this tutorial we'll see how to parse an XML file using external java classes belonging to the NanoXML package. NanoXML is an efficient and easy-to-use XML parser developed by CyberELF.

You can find the source code of this extension in the sfsExtensions/ folder. The file is called xmlReader.as

The book collection

Our example will read an XML file containing a list of developers book that looks like this:

<!-- Title and collection name -->
	<collectionName>My Tech Book Collection</collectionName>
<!-- The list of books goes here --> <bookList> <book title="Head First Design Patterns" author="Freeman, Freeman" year="2004" publisher="O'Reilly" />
<book title="Thinking in Java" author="B.Eckel" year="2003" publisher="Prentice Hall" />
<book title="C++ From ground up" author="H.Schildt" year="2004" publisher="McGraw-Hill Osborne Media" />
<book title="Python in a Nuthshell" author="A.Martelli" year="2003" publisher="O'Reilly" />
<book title="C# Cookbook" author="Teilhet, Hilyard" year="2003" publisher="O'Reilly" /> </bookList>

The Java code

Before we see the actual code of the extension it would be better to quickly see how the NanoXML classes are used in a simple Java application. Here it is the code for reading the above XML file:

import java.util.Enumeration;
import java.util.LinkedList;

import net.n3.nanoxml.IXMLElement;
import net.n3.nanoxml.IXMLParser;
import net.n3.nanoxml.IXMLReader;
import net.n3.nanoxml.StdXMLReader;
import net.n3.nanoxml.XMLParserFactory;

public class XmlReaderExample
	IXMLParser xmlParser; 	
	IXMLReader xmlReader;
	public XmlReaderExample()
		IXMLElement book;
			// Create the XML parser
			xmlParser = XMLParserFactory.createDefaultXMLParser();
			xmlReader = StdXMLReader.fileReader("books.xml");
			// Read file and parse it!
			IXMLElement xmlDoc = (IXMLElement) xmlParser.parse();
			// Get the tag called 
			IXMLElement node = xmlDoc.getFirstChildNamed("collectionName");
			System.out.println("Collection Name: " + node.getContent());
			// Get the tag called 
			node = xmlDoc.getFirstChildNamed("collectionOwner");
			System.out.println("Collection Owner: " + node.getContent());
			// Get the tag called 
			node = xmlDoc.getFirstChildNamed("bookList");
			Enumeration books = node.enumerateChildren();       
			LinkedList tempWordList = new LinkedList();
			while (books.hasMoreElements())
			    book = (IXMLElement) books.nextElement();
			    System.out.println("Title    : " + book.getAttribute("title", ""));
			    System.out.println("Author   : " + book.getAttribute("author", "unknown"));
			    System.out.println("Year     : " + book.getAttribute("year", "unknown"));
			    System.out.println("Publisher: " + book.getAttribute("publisher", "unknown"));
		catch (Exception e)
	public static void main (String[] args)
		new XmlReaderExample();

The first part of the code sets up the necessary objects for parsing the our data file. We create the XmlParser and the XmlFileReader and finally parse the document. The result of this operation is an IXMLElement which represents the root node of our XML data. From there we can start descending in the node structure and reading tag values and attributes. For a complete description of all methods and classes of the NanoXML package you should check their online documentation.

The Actionscript extension

Now that you have an idea of how NanoXML classes are used, we can see how we can use those same classes inside our Actionscript extension:

function init()
        // Create a reference to the Java package
        // This help us building new objects from the nanoxml package.
        // Instead of typing the fully qualified Class name we'll just use:
        // var obj = new nanoxml.SomeObject()
        nanoxml = Packages.net.n3.nanoxml

* Read and parse the XML file
function readTheXmlFile()
        // Setup the xml parser object
        var xmlParser = nanoxml.XMLParserFactory.createDefaultXMLParser()
        // This is the XML Reader:
        // You can use a fileReader, to read the XML from a file
        // or the StringReader, to read the XML from a string
        var xmlReader = nanoxml.StdXMLReader.fileReader("sfsExtensions/data/books.xml")
        // Assign the reader to the parser
        // Finally parse the XML
        var xmlDoc = xmlParser.parse()
        // Get the tag called <collectionName></collectionName>
        var node = xmlDoc.getFirstChildNamed("collectionName")
        trace("Collection Name: " + node.getContent())
        // Get the tag called <collectionOwner></collectionOwner>
        var node = xmlDoc.getFirstChildNamed("collectionOwner")
        trace("Collection Owner: " + node.getContent() + newline)
        // Get the tag called <collectionOwner></collectionOwner>
        var node = xmlDoc.getFirstChildNamed("bookList")
        // book is a java.util.Enumeration object
        var books = node.enumerateChildren()
        // Cycle through each element in the Enumeration
        while (books.hasMoreElements())
                var book = books.nextElement()
                trace("Title    : " + book.getAttribute("title", ""))
                trace("Author   : " + book.getAttribute("author", "unknown"))
                trace("Year     : " + book.getAttribute("year", "unknown"))
                trace("Publisher: " + book.getAttribute("publisher", "unknown"))

function destroy()

function handleRequest(cmd, params, user, fromRoom)

function handleInternalEvent(evt)

The init() function introduces a new object called Packages, which allows us to access any external java class included in the current classpath. In this case the NanoXML classes are found in the net.n3.nanoxml package, so we assign the fully qualified name to a variable to reduce the amount of typing.

If you need to create instances of any class inside the Java2 framework it will not be necessary to use the Packages object, just use the fully qualified class name. For example an ArrayList can be created like this:

var list = new java.util.ArrayList()

The readXml() method contains the same code we've seen in the previous Java example, translated in Actionscript.
In the first three lines we setup the XmlParser and XmlReader object and parse the document. After this section we finally obtain the object representing the root node of our data structure.

On each node you can call one of these methods:

getFirstChildNamed(nodeName)   searches the first occurrence of a sub-node with the same name of the passed argument.
enumerateChildren()   return a list of child nodes.
getAttribute(name, defValue)   return the value of an attribute, defValue is the default value that should be returned in case the attribute is not set.
getValue()   return the node value.

Name: Marco Lapi, a.k.a Lapo
Location: Fossano, Italy
Age: 34
Flash experience: started out with Flash 4 back in 1999
Job: web designer/developer
Website: http://www.gotoandplay.it/
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048