Presentation is loading. Please wait.

Presentation is loading. Please wait.

ActionScript 3 Extensible Markup Language (XML) by Martin Stanhope The University of Bolton.

Similar presentations


Presentation on theme: "ActionScript 3 Extensible Markup Language (XML) by Martin Stanhope The University of Bolton."— Presentation transcript:

1 ActionScript 3 Extensible Markup Language (XML) by Martin Stanhope The University of Bolton

2 What is XML? HTML (HyperText Markup Language) uses predefined tags such as,,, to format information for displaying in web pages. XML (Extensible Markup Language) uses user- defined tags for the purpose of structuring data not for the purpose of displaying it. XML is a markup language used to structure, format and describe data. XML documents are simple text files that are used for exchanging data across the Internet or dissimilar applications.

3 What is an XML document? "The Stranglers" "Golden Brown" "GoldenBrown.mp3" "The Stranglers" "Something Better Change" "SomethingBetterChange.mp3" "The Stranglers" "Tank" "Tank.mp3" playlist.xml

4 XML terminology XML tag - These are user defined and are used to mark the beginning and end of a piece of information. Eg.. XML element - An XML element is a collection of an opening tag, the corresponding closing tag and the information between them, for example, an example of a track element is: "Stranglers" "Something Better Change" "SomethingBetterChange.mp3" Note how this track element contains 3 other elements. XML tree. The top-down listing of elements playlist track artist trackname filename

5 XML objects Qu. How is XML dealt with in ActionScript 3? Ans. By the use of XML objects created from the class named XML. XML objects can be created and dealt with in the following ways: Method 1 Create an XML object and insert the information into it (i.e. 'populate' it) by assigning an 'XML literal' to it. Method 2 Create an XML object by passing an XML string to the XML class constructor. Method 3 Create an XML object (an unpopulated one), and populate it by inserting one element at a time using AS3 statements. Method 4 Create an XML object (an unpopulated one), and fill it with data from an external XML file.

6 XML objects – Using method 1 Method 1- Create an XML object and insert the information into it (i.e. 'populate' it) by assigning an 'XML literal' to it. var student:XML = Joe Bloggs 1234567 ; // Display the XML object including tags and data trace(student);

7 XML objects – Method 1 expanded var students:XML = Joe Bloggs 1234567 Sue Jones 9876543 ; trace(students);

8 XML objects – Method 2 Method 2 - Create an XML object by passing an XML string to the XML class constructor. var firstname = "Joe"; var lastname = "Bloggs"; var idnumber = "1234567"; var str:String = " " + " " + firstname + " " + " " + lastname + " " + " " + idnumber + " " + " "; var student:XML = new XML(str); trace(student);

9 XML objects – Method 3 Method 3 - Create an XML object (an unpopulated one), and populate it by inserting one element at a time using AS3 statements. var student:XML = ; // Create an XML object with a root element student.firstname = ; // Create subelement student.lastname = ; // Create subelement student.idnumber = “ “; // Create subelement trace(student); // Now put some information into the XML object student.firstname = "Joe"; student.lastname = "Bloggs"; student.idnumber = "1234567"; trace(student);

10 XML objects – Method 4 Method 4 - Create an XML object (an unpopulated one), and fill it with data from an external XML file. // Put file name into a string variable var mySampleFile:String = "playlist.xml"; // Create a new URLRequest object to specify the filename (and location) var myRequest:URLRequest = new URLRequest(mySampleFile); // Create a new loader object var myLoader:URLLoader = new URLLoader(); // Tell the loader to load the file as plain text. myLoader.dataFormat = URLLoaderDataFormat.TEXT; // Register the event handler to invoke a COMPLETE event when XML file loaded myLoader.addEventListener( Event.COMPLETE, onComplete ); // Load the XML file myLoader.load(myRequest);

11 XML objects – Method 4 short version The following is the same as the previous slide but in a short version… var myLoader:URLLoader = new URLLoader(); myLoader.dataFormat = URLLoaderDataFormat.TEXT; myLoader.addEventListener( Event.COMPLETE, onComplete ); myLoader.load( new URLRequest("playlist.xml") );

12 XML objects – Method 4 – The event handler The statement to register the event that triggers when the XML file has completely loaded is: myLoader.addEventListener( Event.COMPLETE, onComplete ); An event handler function named onComplete is required to… 1.‘Receive’ the event and assign it to a local variable (named e in this example). 2.Assign the event target data (i.e. the XML text that has been fully downloaded) to an XML object 3.Do something with the data. private function onComplete( e:Event ):void { var myXML:XML = new XML( e.target.data ); trace( myXML ); }

13 XML objects – Method 4 – More useful event handler code var counter:uint = 1; // a counter // Display a list of track names for each ( var myElement:XML in myXML.elements() ) { _displayTXT.appendText(counter + " " + myElement.trackname + "\n"); counter++; // increment the counter by 1 } // Demo of selecting a particular track filename, e.g. the first track is indexed // using the index number zero (as in arrays). _displayTXT.appendText("\n\n" + myXML.track[0].filename + "\n");

14 XML objects – Method 4 – More useful event handler code The above code causes the following to be displayed in the dynamic text field... 1 Golden Brown 2 Something Better Change 3 Tank GoldenBrown.mp3 The code demonstrates the two features you will need for the assignment: The means of displaying a list of available track titles. The means of selecting a track file name from all the track filenames held in the XML object.

15 XML objects – Method 4 – Problems with XML text files What happens if the external XML file doesn't load (perhaps because its content is not a valid XML structure)? To deal with such a situation the event handler has to… try to deal with the event If there is a problem it will generate an error condition which can be caught and dealt with by issuing an error message to the user.

16 XML objects – Method 4 – The try…catch block private function onComplete( e:Event ):void { try { var myXML:XML = new XML( e.target.data ); trace( myXML ); } catch ( e:TypeError ) { //If this section is executed then the downloaded file //could not be converted into an XML object, //possibly due to incorrect formating of the XML //code in the text file trace( "CATCH: Error. Could not parse text into XML"); trace( "CATCH: Error. " + e.message ); } }//End of event handler onComplete

17 Workshop Exercises Exercise 1 – Trials with the first 3 methods of creating XML objects Method1.as / Method1.fla Method1b.as / Method1b.fla Method2.as / Method2.fla Method3.as / Method3.fla Exercise 2 – Trials with method 4, creating an XML object using external XML data files (e.g. student-data.xml and playlist.xml) XMLloader1.as / XMLloader1.fla XMLloader2.as / XMLloader2.fla XMLloader3.as / XMLloader3.fla XMLloader4.as / XMLloader4.fla XMLloader5.as / XMLloader5.fla Exercise 3 – Creating an XML data file of your own then writing an AS3 application to load in the file and to display its data in a dynamic text field.


Download ppt "ActionScript 3 Extensible Markup Language (XML) by Martin Stanhope The University of Bolton."

Similar presentations


Ads by Google