Join Todd Perkins for an in-depth discussion in this video Using XML methods, part of ActionScript 3.0: Working with XML.
Now let's talk about working with some built-in XML methods that allow you to work with XML data. If you are following along you can just use the same file that we have been working with throughout this chapter or you can open up 03_XML_methods.FLA in the Chapter 2 folder in the exercise files folder. Just go to the first keyframe of the Actions Layer and open up the Actions Panel. Right below where we created the photographer element, let's type image.appendChild. The appendChild method allows you to put a child element at the very bottom of the other children elements.
So what we will do is inside the parenthesis for the appendChild method I am going to create a new XML element called Description and inside of that element I am going to write a description. I am just going to type this as a description. Make sure to close out the parenthesis and test the movie and you want to just trace the whole XML element then look at description as the bottom XML element inside of our image element. Another method that's built into Flash is called PrependChild that puts a child element before all other children. So if I change appendChild to PrependChild and test the movie then I see the description is above photographer in the output window.
Below the PrependChild line of code let's use another XML method. This one is called insertChildAfter, this accepts two parameters child 1 and child 2. The first parameter refers to the objects that you want to insert a child after. So let's say I wanted to insert something after the description so I am going to type image.description and then for the next parameter I am going to create a new element, let's call this image title. And for the element I am just going to type, This Is A Title.
I am going to close out image title. Make sure to close out the parenthesis and then test the movie and then I get image title right below description. Let's say I wanted to put a child element before another element. Instead of using insertChildAfter I can use insertChildBefore. Test the movie and then the image title will be before the description. Remember that if you want to look up other XML methods you can always look at the XML class in Flash Help.
To do that you can just highlight XML on your code and press F1 on the keyboard and Flash will take you in Flash Help directly to the XML class. Then you can look up the other methods there. So there's a look at some of the methods that you can use that are built into Flash to work with XML data.
- 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