Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

New Flash: How to Integrate Flash with Office 2003

Is it possible to combine the expressive and analytical power of Office with the display slickness of Flash? Yes, and it's quite easy, even for a person with limited Flash abilities (a category including your author).


advertisement


ntegrating Flash or any other XML-supporting application with Office 2003's XML capabilities is easy. In fact, when I was noodling around trying to come up with demonstration apps, I was somewhat dismayed to find that I was spending all my time in Flash; there weren't really any challenges in Office. And then I remembered: That's the point!

Shortly after publishing "XML Integration Between Office Apps" on DevX, I received an email asking if it was possible to combine the expressive and analytical power of Office with the display slickness of Flash. Yes, and it's quite easy, even for a person with limited Flash abilities (a category including yours truly).



To program Flash, you'll need a Flash compiler. The vast majority of users will use Macromedia's offerings (I use Flash MX 2004 Professional), but I recently became aware of a product from a company called Swfsoft (recently acquired by Madcap Software) that directly supports XML to specify Flash movies. Because most people will use Macromedia's products, I didn't include a sample. Throughout the rest of this article, "Flash" refers 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 significantly from those Microsoft uses in the Office suite and provides for .NET developers. This is not a show-stopping 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's legal and what isn't.

Having said that, Macromedia's parser is more lax than any I've used since the late '90s, and it doesn't support the validation and navigation features that a Visual Basic .NET or C# programmer might expect. For an Office developer, the most significant thing is that Macromedia's parser doesn't support W3C XML Schema, so the types of work one associates with Word's XML Structure taskpane or Excel's XML Maps is unavailable within Flash.

It's true that working with Schema within Office can be difficult, but the recently updated Excel XML Toolbox is a tremendous aid—an absolute must-have when working with Excel and XML. As can be seen 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 next 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 initially, for those who've been relying on Flash's Timelines to create animations, it can be 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 analogous 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 are actually similar to the way things worked in the old days of line-numbered BASIC—control generally moves sequentially forward but jumping commands allow you to GOTO anyplace you want.)

Movie 2 quickly shows the steps: I use the drawing tools to create a shape on the Stage, which I then select. At this point, that 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's important to select the checkbox "Export for ActionScript" and set the "Linkage Identifier" to the type of the class ("Symbol 1").

I can now create as many instances of that type as desired, each one of which can be individually manipulated. To show that, I delete the shape (now a Symbol) that I'd just drawn, and drag a Symbol 1 from the Library and place it on the Stage. I name the instance of the Symbol mySymbol. Notice that I've drawn a couple of lines to mark the origin of Stage and that I've placed mySymbol considerably away from those lines.

Now it's time to do some coding. I open the "Actions" panel, choose Scene 1: Layer 1: Frame 1 and paste in the code shown in Listing 2. The first line, xml = new XML(); creates a new object of type xml—the XML parser included in Flash's library. The second line sets the ignoreWhite property of the parser to true, otherwise, we'd end up with an overly complex tree that included nodes for the spaces and new lines that are part of most "pretty printed" XML. The third line loads the file that contains the data shown in Listing 1.

The block of code that dominates the listing is an event-handler for the xml object's onLoad event. If the load is successful (common reasons for failure would be a missing file or ill-formed XML within that file), the xml object now contains a tree-like data structure whose contents correspond to the loaded XML text.

Flash's XML class is similar to, although not as capable as, the XmlDocument class of the Base Class Library. Flash does not have support comparable to the .NET Framework's for serializing XML to and from instances of particular types: if you want to do that type of thing (which you almost certainly will), you'll have to write your own code for navigating the XML DOM and transforming it into objects and properties. In this case, we're just hard-coding our navigation "into" the DOM and reading the value of the two nodes that we happen to know are labeled "ns1:X" and "ns1:Y".

Just as the xml object is a tree-like data structure, so too are the Stages of a Flash movie. The _root object is the top-level stage and, because of our work earlier, it has a child called mySymbol of type Symbol 1. The position of mySymbol is specified with its _x and _y properties, which we set by using parseInt() on the text values of the appropriate XML nodes. When the movie is run, the result is as shown in Figure 1.


Figure 1.
Running the simple movie.

Note 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) but instead is located at 10x10 (you'll also note that the point from which the Symbol 1 class takes it's location is not hard-coded to be the upper-left corner as a Windows Forms programmer might expect, but is something that can be manipulated in Flash. In this case, it's the center of the circle).

The first example showed the most basic use of driving Flash from Office XML, manipulating an existing symbol. More commonly, you'll 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, investing a little time to develop a more sophisticated XML deserialization pattern will really pay off in the long run.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap