Join Todd Perkins for an in-depth discussion in this video Loading XML from an external file, part of ActionScript 3.0: Working with XML.
In this chapter we are going to be working with loading XML from an external data source. If you are following along I want you to open up gallery.xml in the Chapter 3 folder instead of a text editor. I am using TextWrangler here but it doesn't matter which text editor you use. If you don't have access to the exercise files, create a plain text document and save it as gallery.xml, copy down all the data in this file. This is a file that we are going to be loading into Flash in this exercise. Let's just quickly take a look at it. The first line has information about the XML, then we have the main gallery element as the root element, some processing instructions, a comment then we have two child elements and they are both called Image and they each have a 3 child elements title, description and photographer.
Photographer has an attribute of ID and two elements' name and location. So let's take a look at how to load this data into Flash. You are going to tab over to Flash right now and if you have the exercise files, just open up 01_Loading_External.fla in the Chapter 3 folder and if you don't you can just create a dynamic text fill on the stage and give it an instance name of info_txt. Let's go to the first keyframe in the Actions Layer and open up the Actions Panel and the code I already have here just imports all the component classes and I created a variable called info_txt to represent a text area that's buried within a few movie clips.
So I am going to go down a few lines, I am going to create a variable to represent our XML. I am going to call this gallery_xml. I am just going to data type it to XML then I will create our XML request and call this XML Req as a URLRequest and we are going to set the value inside the parenthesis here equal to gallery.xml. On the next slide let's create our URLLoader and we call it XML loader.
And then I am going to go down a few lines and make the XML loader load our XML file. Then we will add an EventListener to listen for when the files finish loading. That's event.complete and we will run a function called xmlLoaded. So let's define that function and then inside of the function we will set the value of our gallery.xml object so Gallery_XML so that equal to a new instance of the XML class and then we will pass in xmlloader.dat.
That's the property that holds the data that's loaded when the URLLoader loads external data. Then let's put that data in our text field so go to the next line of code and type info_txt.text and we will set that equal to Gallery_XMl.2 XML string. Now there are two methods of the XML class that generate a string from XML data. There is two XML string and a two-string. If you want to know the difference you can just highlight two XML string and press F1 on your keyboard to open up Flash Help and then you can read about how there is a minor difference and basically the XML string contains all of your XML data and then two string eliminates parent elements if you have simple XML content.
You can see an example if you scroll down a little bit. So let's test movie and see what we get in our text field. So it starts with gallery and we have all of our image elements and everything. You will notice that we don't see the information about the XML file. That actually gets removed when the XML gets parsed. We also don't see processing instructions or XML comment. That's because by default those elements are removed when the XML is parsed. You can modify a few properties to show those elements. We will take a look at how to do that in the next movie.
So there is a look at how to load XML from an external XML file.
- Understanding XML data
- Creating XML in ActionScript 3.0
- Working with external XML data
- Building a reusable RSS reader class
- Using PHP with XML
- Working with blogs
- Working with Flickr images using the Flickr API
- Working with podcasts