Creating XML solutions for Macromedia Flash MX
Macromedia is moving into the application development arena with many of the new features available in Flash 5.0 and MX. One of these features is the XML integration in Flash. Even if you're not a Flash designer, you can take your knowledge of XML and put it to the test in Flash.
Without much effort, you can acquire enough knowledge of Flash to create a complete solution. Flash is based on a movie philosophy in that events usually occur along a timeline, which is divided into frames. However, with ActionScript, you can handle events and navigate a movie without many frames composing the whole movie. In my example, I create a phone directory solution with only one frame.
ActionScript is based on the ECMA-262 standard, so it closely resembles JavaScript. If you're familiar with JavaScript, you won't have any problems adapting to ActionScript.
The phone directory solution that I'll create is a very simple one. You enter the last name or part of the last name of the person for whom you are looking. This last name data is then sent to an Active Server Page (ASP) that uses the query string data in the request to search a Microsoft Access database for a match. The recordset returns, converts to XML, and returns as XML to the Flash movie. The Flash movie then parses the XML and displays the result set.
The Flash movie only contains a text input area for entering the last name data and button to initiate the search. The results are instantiated as individual movie clips from a movie clip symbol. (Instead of going into a complete Flash tutorial, you'll find a link to the source code at the end of this article.)
Flash provides an XML object class for creating XML solutions. The load() method on the XML object allows you to download data from a remote host. When the data is finished downloading, the onLoad event of the XML object occurs.
You can provide an event handler for the XML object by setting this onLoad event to a valid function name. Flash's XML object doesn't provide a great deal of functionality with the retrieved XML. However, with a bit of intelligent design, you can create some witty solutions.
Here's the onLoad event handler for the XML object:
function myXML_onLoad() { for (var p = 0; p < pcount; p++) { removeMovieClip(_level0["phone" + p]); } for (var i = 0; i < myXML.childNodes.length; i++) { var node = myXML.childNodes[i]; attachMovie("phone", "phone" + i, i); var newMovie = _level0["phone" + i]; newMovie._x = lblHeading._x; newMovie._y = (lblHeading._y + lblHeading._height) + (i * (lblHeading._height + 2)); for (var n = 0; n < node.childNodes.length; n++) { var child = node.childNodes[n]; newMovie[child.nodeName] = child.childNodes[0].nodeValue; } } pcount = i; }
The first for() loop removes the existing movie clips from the previous result set. The next for() loop iterates through each childNode in the XML object (myXML). A new instance of the phone movie clip symbol is added and positioned on the stage with each loop. The phone movie clip symbol contains several component variables that match the nodeNames of each childNode of the XML document. These are used to set the text of each movie clip instance. One thing to notice is the child.childNodes[0].nodeValue property call. When a normal node is returned with text between the start and end tag, that text is known as a text node. You must refer to the child node of the element to get the contents of that element. Here's a sample of the XML that is being returned:
<row> <first_name>John</first_name> <last_name>Public</last_name> <phone>800-555-1212</phone> </row>
In order to get the XML result set, a button was added to the stage with the following event handler:
btnOK.onRelease = function() { myXML.load("getInfo.asp?s=" + m_txtLName); }
This function requests XML from the getInfo.asp page. The last name is passed in as a query string.
Now for the ASP to handle the request:
Response.Expires = -1 Response.Buffer = True Dim conn, rs, xml, xsl Set conn = Server.CreateObject("ADODB.Connection") conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("test.mdb") & ";User Id=admin;Password=;" conn.Open Set rs = Server.CreateObject("ADODB.Recordset") Set rs.ActiveConnection = conn rs.Open "SELECT * FROM Phone WHERE last_name LIKE '" & Request.QueryString("s") & "%'" If Not rs.EOF Then Set xml = Server.CreateObject("MSXML2.DOMDocument") Set xsl = Server.CreateObject("MSXML2.DOMDocument") xml.async = False xsl.async = False rs.Save xml, 1 'adPersistXML xsl.load Server.MapPath("ADOGeneric.xsl") Response.Write xml.transformNode(xsl) End If Set xsl = Nothing Set xml = Nothing Set rs = Nothing conn.Close Set conn = Nothing
This code grabs the recordset, saves it in XML format, and transforms that XML into usable XML. The XML is then sent out to the Response buffer.
If you'd like the source code to this solution, please copy and paste this URL into your browser: http://www.geocities.com/phil_perkins_1/Flash/flash.zip
Phillip Perkins is a contractor with Ajilon Consulting. His experience ranges from machine control and client/server to corporate intranet applications.
Talk Web development with your peers
Get a feel for the community consensus on issues that are important to you, and offer your views in the Web Development Forum. If you'd like to offer feedback about this column, please send us an e-mail.
Develop skills and know-how to support XML
|
|
Make XML a corporate investment that pays off with the skills to manage and support the XML environment. Learn about XML basics, programming, databases, Web services, and security; plus, get sample XML coding. Order your copy of Developer's Guide to XML today! | |
Filter dates with the Flash MX Calendar Component The Flash MX Calendar Component adds a nice, rich media touch to your Web site. Find how to take full advantage of this advanced Flash feature.
XML drives this dynamic Flash MX application download With the introduction of XML in Flash MX, developers can create more efficient, dynamic applications. To see these features in action, download the source code for this sample application. |