Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Once you've loaded XML data into Flash, you can work with that data using dot syntax. That's part of the benefit of XML being native in ActionScript 3.0. Here if I test the movie, you can see the XML data in the Output window. Let's take a look. So we have the images root element and then inside of there are image child elements, and each one of those elements has an attribute called file that has the file name of an image.
Now, let's say we want to access the file name of each image. Let's go back to our code and see how to do that. We'll just leave this code in a trace statement for now. We'll look at applying it to an actual image later on. So after imagesXML, you can type a dot and then type image. Now if you test the movie right now, here is what you'll get. Let's look in the Output window.
This actually gives us a list of all the child elements named image. So if we go back to our code, imagesXML represents that outer XML element. It has a child called image. When we reference image, Flash gives us a list of XML objects. The datatype of this list is called XMLList. It looks like this: XML in all caps, and then List with the capital L. If you want to know everything you can do with an XMLList, highlight it, and press F1 on your keyboard.
For now, we're going to look at something basic. Let's say we wanted to only access one object in a XMLList. After image, we can use square brackets, just like when you're working with an array. Remember that when you're working with an array, the first index is 0 and not 1. So type in 0, test the movie, and then we don't get anything. Now, that actually is not a bad sign. The reason for that is that the image element doesn't have any child elements, or child data at all.
So that's what we're actually seeing here. When we reference that image, we're seeing any children or any data that's inside of that image element, and right now there's not anything. The data that we want to access is inside of an attribute of that element. So again, we're accessing the first image element, and when we reference that image by itself, we don't see anything just yet. After the close square bracket, type a dot and then type an at symbol, this stands for attribute, and then type in the attribute name that you want to access.
That's called file. So inside of the image element, there is an attribute called file, and this is how you access it. Test the movie, and there is the data that we're looking for. So to review, when you create a new XML object with loaded data, that XML object represents the root of the loaded data. So in this case, it would be the images element. When you access child elements that have the same name, Flash gives you an XMLList, which is different from an XML object.
You can access single elements in that list by using square bracket notation and an index number, just like when you're working with an array. Once you have access to an element, you can access attributes of that element by typing a dot and then by typing at symbol and the attribute name. So by using this line of code, we're able to access specific names of files that we want to work with. So remember that E4X enables you to work with XML data using dot syntax.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 103120 Viewers
61 Video lessons · 89778 Viewers
71 Video lessons · 73407 Viewers
56 Video lessons · 104960 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.