Join Todd Perkins for an in-depth discussion in this video Understanding E4X syntax, part of ActionScript 3.0: Working with XML.
Now in this chapter we are going to be working a lot with creating XML data using ActionScript 3.0. If you are following along I am working in 01_understanding_E4X.FLA in the Chapter2 folder in the Exercisefiles folder. If you don't have access to the Exercise files just create a text field on the stage, make sure it's a dynamic text field and give it an instance name of info_txt. Let's look at the first keyframe within the Actions Layer and open up the Actions Panel. In my code I am importing all the component classes and I have created a variable called info_txt to represent a text area component.
I did that so I don't have to type out the full path of the component every time I want to reference it. If you simply have a text field on the stage named info_txt you don't need to write any of this code here. Let's go down a few lines and talk a little bit about E4X. E4X refers to ECMAScript for XML. That essentially means that in ActionScript Flash uses XML as a native data format which means we can create XML data in ActionScript the same way that we create XML data in an XML file and there are also a few ways you can create XML data.
It's even simpler than creating XML in an XML file. Let's start out by creating a variable. I am going to call this Image. This is going to represent an image in our gallery so I am going to type colon. I am going to data type this to XML. I am going to set it equal to a new instance of the new XML class. Inside of the parenthesis of our constructor function we are creating a new instance of the XML class. We are going to create an Image element. So I am going to type an open bracket I am going to type image a forward slash and then a close bracket. This way of typing is the shorthand way to sort of open and close an element at the same time.
So now we have our XML and our parent element is Image. Just get onto the next line and we can add elements and values to those elements in our XML in the same way that we add a value to an object. Let me show you how that works. Image. and let's say I type .filename so file lowercase f name with capital N all one word, space equals space and then in quotes I am going to put the path to some imaginary file.
So I am going to call this pickone.jpeg. Let's get on to the next line. I am going to type image.photographer space equals space and then in quotes I am going to put the name of photographer I am just going to make up some name photographer1. Now at this point let's take a look at what our XML data looks like. I am going to do this in a trace statement. Go down a few lines and I am going to type trace and then in the parenthesis we are going to pass in image. That's going to trace our XML data.
Let's see we get an output window when we test the movie. So we can test the movie by pressing Command Return or Ctrl+Enter and since you are probably really familiar with that keyboard shortcut that's the last time I am going to say, so check in the output window and you will see our XML. We have image, opening and closing tag and inside of that element we have a Filename element and Photographer element. So let's say what we want to do is take some of our XML data and display it in the text field on the stage.
I am just going to delete this trace statement. I am going to put the Photographer element inside of our text field. So I am going to set the text property of info_txt to image.photographer. So let's test the movie and then we see the photographer's name in the text field on the stage. So you can see that we can access XML data using E4X in the same way that we access any other data in Flash that's using .syntax and that's the beauty of E4X. Throughout the rest of this chapter we will take a look at some more easy ways to reference data using E4X.
- 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