Download presentation
Presentation is loading. Please wait.
1
XML and Flash © University of South Wales
2
Communicating with external sources
Adobe Animate (Flash) supports a number of methods of communicating with external data sources. XML URLRequest and URLLoader classes, and then listen for the complete event (AS3) PhP/MySQL / Coldfusion etc Web Services © University of South Wales
3
XML Recap XML is a simple but very flexible text information system
Originally developed to meet the challenges of large-scale electronic publishing XML is widely used to exchange data on the web and elsewhere including Flash documents © University of South Wales
4
XML Vs. HTML XML is designed to carry data and describe such data whereas HTML was designed to display data and to focus on how it looks HTML is about displaying information XML is about describing information in a universal format © University of South Wales
5
XML Example <?xml version="1.0"?> <MUSIC_COLLECTION> <MUSIC> <ALBUM>Lovebox</ALBUM> <BAND>Groove Armada</BAND> <ALBUMCOVER>groovearmada.jpg</ALBUMCOVER> <LABELCODE> </LABELCODE> <PRICE Currency="£">10.25</PRICE> </MUSIC> <ALBUM>My Way</ALBUM> <BAND>Frank Sinatra</BAND> <ALBUMCOVER>franksinatra.jpg</ALBUMCOVER> <LABELCODE> </LABELCODE> <PRICE Currency="£">12.00</PRICE> </MUSIC_COLLECTION> © University of South Wales
6
XML Structure An XML file consists of tags within tags
Known as Children inside Nodes Allows for data to be stored in a structured manner. There are two albums in the music collection The first album is Lovebox The last artist in the collection is Frank Sinatra © University of South Wales
7
Writing XML Like actionscript, XML has its own strict syntax rules which must be followed if the file is to be read at all by Flash. Incorrectly formatted files will often simply fail to be read. To check the structure of your XML file and/or edit it, you can use XMLSpy (a commercial product) © University of South Wales
8
Why XML and Animate? Animate (Flash) is capable of reading in and processing a structured XML file The processed data can then be used to control animation in Animate or to fetch data in to the Rich User Interface. E.g. If you have an XML file containing data for a quiz then Animate knows how many questions it contains Or access product Information from Amazon and display this data in Animate. © University of South Wales
9
XML and Animate XML in AS3 is very different from AS2
One of the major changes in AS3 is how you deal with XML content (i.e.in AS2 you had to use more complex statements to find a specific node in the data.) One of the introductions is Ecmascript for XML, known as E4X for short. E4X is a new class which introduces a new way to look at the XML strings using native actionscript objects. rence/actionscript/3/XML.html Ref. Ref. Ref. © University of South Wales
10
XML and Animate: example (AS3)
<Books> <Book ISBN=" "> <title>Sherlock Holmes</title> <author>Arthur Conan</author> </Book> <Book ISBN=" "> <title>may brown</title> <author>sue Haskett</author> <Book ISBN=" "> <title>The great gatsby</title> <author>F. Scott Fitzergerald</author> </Books> each box in the above image = a node. main root node = Books four child nodes = Book. The Book node contains the ISBN information = attribute. book's title and author information is stored in child nodes aptly called title and author. Ref for above: Ref. Ref. © University of South Wales
11
XML and Animate: AS3 In order to load the XML data in Animate we need to do the following: Create a variable to hold an instance of the XML Class. Create an instance of the URLLoader Class to load the XML file. Pass the content of the XML file to the XML instance variable once the file has completed loading. Ref. © University of South Wales
12
XML and Animate: AS3 In order to load the XML data in Animatewe need to do the following: 1. Create a variable to hold an instance of the XML Class. var myXML:XML; Ref. © University of South Wales
13
XML and Animate: AS3 In order to load the XML data in Animate we need to do the following: 2. Create an instance of the URLLoader Class to load the XML file. var myXML:XML; var myLoader:URLLoader = new URLLoader(); myLoader.load(new URLRequest( “book.xml")); Ref. The URLLoader Class is the class responsible for loading all binary and textual data.Once we create an instance of the URLLoader Class, we can use its .load method to load the XML file. © University of South Wales
14
XML and Animate: AS3 In order to load the XML data in Animate we need to do the following: 3. Pass the content of the XML file to the XML instance variable once the file has completed loading. var myXML:XML; var myLoader:URLLoader = new URLLoader(); myLoader.load(new URLRequest( “book.xml")); myLoader.addEventListener(Event.COMPLETE, processXML); Ref. …. to process the XML file we must make sure that it has been fully Loaded…to do that we need to create a listener that checks for the loading process to complete and then process the XML file. © University of South Wales
15
XML and Animate: AS3 var myXML:XML; var myLoader:URLLoader = new URLLoader(); myLoader.load(new URLRequest(“book.xml")); myLoader.addEventListener(Event.COMP LETE, processXML); function processXML(e:Event):void { myXML = new XML(e.target.data); trace(myXML); } This code will trigger the listener function processXML when the XML file finishes loading. To create the contents of this function… we need this function to assign the contents of our XML file as the XML data of our XML instance variable: e.target refers to the object that loaded the XML file…{{event object sent to the function to retrieve the actual data from the loaded file.}} Ref. © University of South Wales
16
XML and Animate (AS3) To get the authors from the data
trace (myXML.Book.author); <author>Arthur Conan</author> <author>sue haskett</author> <author>F. Scott fitzergerald</author> To get the authors from the data without <author> trace (myXML.Book.author.text()); Ref. Ref. Ref. © University of South Wales
17
XML and Animate (AS3) To get an attribute you use the attribute function trace (myXML.Book.attribute (“ISBN”)); Or a shortcut is trace Ref. Ref. Ref. © University of South Wales
18
XML and Animate (AS3) To get the first author (without author tags)
trace (myXML.Book.author.text()[0]); To get the second author (without author tags) trace (myXML.Book.author.text()[1]); To get all the children nodes trace(myXML.*); Ref. Ref. Ref. © University of South Wales
19
XML and Animate (AS3) To drill down and access the text value of each of the Book node trace(myXML.Book.*); To get ISBN of the first book To get book title for ISBN ")); Ref. © University of South Wales
20
XML and Animate (AS3) .length() method = can be used to count the number of children a node has or a number of a specific type of children a node has. For example, to check how many author nodes are present in our book selection we can use the length() method this way: trace(myXML.author.length()); Ref. © University of South Wales
21
XML and Animate (AS3) We can also easily count the number of all children in our XML file regardless of type by using the asterisk sign : trace(myXML.*.length()); Ref. © University of South Wales
22
XML and Animate (AS3) A loop can be used to cycle through the XML file to get the content of all the nodes. function processXML(e:Event):void { myXML = new XML(e.target.data); for (var i:int = 0; i<myXML.*.length(); i++){ trace("The ISBN is " + + " and it's author is " + myXML.Book.author[i];) }; } Ref. © University of South Wales
23
To display XML in Animate (AS3)
Use the toXMLString () method to return a string representati on of the XML object regardless of whether the XML object has simple content or complex content. function processXML(e:Event):void { myXML = new XML(e.target.data); textArea.text = myXML.Book.author.text()[0].toXMLString(); Ref. © University of South Wales
24
Working example(AS3) See main_example in xml_as3.0 folder on BB
var myXML:XML; var myLoader:URLLoader = new URLLoader(); myLoader.load(new URLRequest(“book.xml")); myLoader.addEventListener(Event.COMPLETE, processXML); function processXML(e:Event):void { myXML = new XML(e.target.data); textArea.text = xml.Book.author.text()[0].toXMLString(); } See main_example in xml_as3.0 folder on BB Ref. © University of South Wales
25
Reusability.... Discussion
Define: Ability of a item that allows it to be used repeatedly unlike a disposable item( Reusability: capable of being used again or repeatedly ( © University of South Wales
26
Resources http://www.developphp.com/list_flash_video.php
Extra: © University of South Wales
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.