Join Todd Perkins for an in-depth discussion in this video Accessing elements and attributes using descendant selectors, part of ActionScript 3.0: Working with XML.
Now let's talk about working with a little bit more complex XML hierarchy. If you are following along I am working in 05_Accessing_Descendants.fla in the Chapter 2 folder in the exercise files folder and of course you can also use the same file you have been working with throughout this chapter. Let's go to the first keyframe in the Actions Layer and open up the Actions Panel and the file looks a little bit different than it has till this point that's because I just clean it up just a little bit to make it a little bit more obvious what our XML looks like. So we have image __________ 00:28 filename, the image title, the description and then I have kind of isolated the photographer here.
So let's say we are going to have more information about the photographer so not just photographer's name but maybe the photographer's location where the photographer lives and maybe some other information about the photographer. It makes sense then to have the photographer have its own XML tree or XML hierarchy. So instead of image.photographer=photographer1, I am going to type image.photographer.name=photographer1 then I am going to go to the next line and then I am going to reference that photographer element and I am going to create a new element called Location and I am going to set that equal to Kentucky.
So let's say this photographer is from Kentucky. So let's test the movie now and we will see that photographer has its own hierarchy too so photographer has two child elements name and location. Now let's say we wanted to reference name or location. What I am going to do is I am going to go to the trace statement and I am going to trace image.photographer.name and then test the movie and so we can access the photographer's name in that way.
But one of the great things about E4X is something called the Descendants Operator. The Descendants Operator allows you to access any descendant element from the parent element and what that means is that the grandfather element or the very outmost parent element can access children or grandchildren using the same syntax. So what I am going to do is just write photographer here in the trace statement so that image..name so what this is going to do is it's going to tell Flash to look for an element called Name as a child or a grandchild or a great grandchild etc. of our image element.
So now when we test the movie we can refer directly to photographer1 through this descendant selector. I can also refer to location using the same method so I will test the movie and there is Kentucky. So there is look at how to access descendants using our descendant selector. Now what's really great about the descendant selector is that it doesn't just work for elements it also works for attributes. So let's say we wanted to give our photographer an ID for example.
So right before where we set the photographer's location I am going to type image.photographer.attributeID and I will set a value for that something along the lines of 001 or something and let's say I want to access that ID, I can just trace image..attributeID. I test the movie and I get the result in the output window. So now you can search through all of your XML data using a simple descendant selector.
- 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