|
Integration of Flash or any other XML-supporting application with Office 2003's XML capabilities is quite easy. When I was noodling around trying to come up with demonstration apps, I was disturbed to find that I was spending all my time in Flash; there were no challenges in Office. But then I remembered that that is the point!
Soon after publishing "XML Integration Between Office Apps" on DevX, I received an email asking whether it was possible to combine the expressive and analytical power of Office with the display slickness of Flash. It is, and quite easily, even for a person with limited Flash abilities.
To program Flash, you will require a Flash compiler. Majority of users will use Macromedia's offerings (I use Flash MX 2004 Professional), but I recently came to know of a product from a company called Swfsoft (recently acquired by Madcap Software) which directly supports XML to specify Flash movies. Because most people will use Macromedia's products, I did not include a sample. Throughout the rest of this article, "Flash" will refer to the Flash MX 2004 designer and its runtime engine, Flash 7.
Office XML To Flash Basics
The major issue in using XML from Flash is that the parser that Macromedia uses to interpret an XML-formatted text file differs greatly from those Microsoft uses in the Office suite and provides for .NET developers. This is not a big problem—one of the major features of XML interoperability is that different parsers can be used and that the specification is so simple (compared to, say, the C++ programming language) that there's not really any ambiguity about what is legal and what is not.
Macromedia's parser is more lax than any I have used since the late '90s, and it does not support the validation and navigation features that a Visual Basic .NET or C# programmer will expect. For an Office developer, the most important thing is that Macromedia's parser does not support W3C XML Schema, so the types of work one associates with Word's XML Structure taskpane or Excel's XML Maps is not there within Flash.
It is true that working with Schema within Office can be tough, but the recently updated Excel XML Toolbox is an excellent aid—an absolute necessity when working with Excel and XML. As is shown in Movie1, the "Build Schema" capability of the Toolbox creates a valid XML Map from selected cells virtually instantly. This data can be exported to Flash-consumable XML by choosing "Save as..." and "XML Data". The resulting file is shown in Listing 1.
In Flash development jargon, the following step is to create a Symbol that is a MovieClip, place it on the Stage, name the instance, and then add ActionScript to load the XML file and modify the instance's values based on the XML.
For advanced
Flash development, these steps will become second nature, but in the beginning, for those who've been relying on Flash's Timelines to create animations, it can be a little confusing. Flash can be programmed two ways: visually, relying on the Stage and Timelines, or with code, emphasizing ActionScript (which is essentially EcmaScript, a.k.a. JavaScript).
When coding Flash, a Flash "Movie" is similar to a Windows Forms "Control"—an instance of an Object class that combines data and behavior, including visual properties. The Stage and Timeline don't have similar analogues; the Stage is something like a Form but Timelines are something like a method and something like a form of flow control. (Timelines have similarity with the way things worked in the old days of line-numbered BASIC—control generally moves sequentially forward but jumping commands allow you to navigate to any place you want.)
Movie 2 quickly shows the steps : I use the drawing tools to create a shape on the Stage, which I then choose. This shape is not a scriptable Object—I have to use the menus (or press the F8 key) to turn the selected shapes into a Symbol. Showing great imagination, I name the type of the class Symbol 1. When you do this, it is crucial to select the checkbox "Export for ActionScript" and set the "Linkage Identifier" to the kind of the class ("Symbol 1").
Now I can create as many instances of that type as I wish, each one of which can be manipulated individually. To show that, I delete the shape (now a Symbol) that I'd just drawn, and drag a Symbol 1 from the Library and put it on the Stage. I name the instance of the Symbol mySymbol.
Now it's time for some coding. I open the "Actions" panel, select Scene 1: Layer 1: Frame 1 and put in the code shown in Listing 2. The first line, xml = new XML(); creates a new object of type xml—the XML parser that is included in Flash's library. The second line sets the ignoreWhite property of the parser to true. The third line loads the file that has the data shown in Listing 1.
The block of code that is of most importance in the listing is an event-handler for the xml object's onLoad event. If the load is smooth, the xml object now has a tree-like data structure whose contents correspond to the XML text that was loaded.
Flash's XML class is similar to the XmlDocument class of the Base Class Library. Flash does not have support comparable with the .NET Framework's for serializing XML to and from instances of particular types: for that you'll have to write your own code for navigating the XML DOM and changing it to objects and properties. Over here, we're just hard-coding our navigation "into" the DOM and reading the value of the two nodes that we know are labeled "ns1:X" and "ns1:Y".
Just as the xml object is a tree-like data structure, so also are the Stages of a Flash movie. The _root object is the top-level stage and, because of our previous work, it has a child called mySymbol of type Symbol 1. The position of mySymbol is denoted by its _x and _y properties, which we set by using parseInt() on the text values of the corresponding XML nodes. When the movie is run, the result is as shown in Figure 1.
It should be noted that mySymbol is no longer at the offset from the origin to which we dragged it (192 x 52.35 according to the debug trace). Instead, it is located at 10x10 .
The first example displayed the most basic use of driving Flash from Office XML, manipulating an existing symbol. You may often want to create new Symbols, place them on the Stage, and manipulate them according to the provided XML. While this could be done with the top-down, hard-coded approach to navigation used in the first example, spending a little time to develop a more sophisticated XML deserialization pattern is really worth it in the long run.
|