8.7 Tutorials: using Java classes in Actionscript
» Introduction
The Server Side Framework 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:
<bookCollection> <!-- Title and collection name --> <collectionName>My Tech Book Collection</collectionName> <collectionOwner>Lapo</collectionOwner>
<!-- 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 Nutshell" author="A.Martelli" year="2003" publisher="O'Reilly" />
<book title="C# Cookbook" author="Teilhet, Hilyard" year="2003" publisher="O'Reilly" /> </bookList>
</bookCollection>
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; try { // Create the XML parser xmlParser = XMLParserFactory.createDefaultXMLParser(); xmlReader = StdXMLReader.fileReader("books.xml"); xmlParser.setReader(xmlReader); // Read file and parse it! IXMLElement xmlDoc = (IXMLElement) xmlParser.parse(); // Get the tag calledIXMLElement 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()); System.out.println(""); // 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("---------------------------------------------------"); 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")); System.out.println("---------------------------------------------------"); System.out.println(""); } } catch (Exception e) { e.printStackTrace(); } } public static void main (String[] args) { new XmlReaderExample(); } }
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 readTheXmlFile() } /* * 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 xmlParser.setReader(xmlReader) // 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")) trace("-------------------------------------") } } function destroy() { // } function handleRequest(cmd, params, user, fromRoom) { // } function handleInternalEvent(evt) { // }
var list = new java.util.ArrayList()
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 |
doc index |