Join Todd Perkins for an in-depth discussion in this video Exploring an XML file, part of ActionScript 3.0: Working with XML.
- View Offline
Now let's take a look at an actual XML file. If you're following along and you have access to the exercise files, open up the exercise files folder, find gallery.XML in the Chapter 1 folder. You can open up that file in any text editor. I'm going to open it up in Text Wrangler. Now if you don't have access to the exercise files, don't worry. You can just follow along and I'm going to walk through how this file is set up. So don't worry about copying down any of this information. So the first line of code here is what's called a processing instruction in XML. This is actually a special processing instruction that contains information about this XML file, including the XML version used and the type of encoding attribute used.
Below that we have the body of our XML file. Now this may look very familiar to you if you're familiar with HTML. We have an opening tag, some gallery here. In XML you can actually use your own names so the content has more meaning. So I see an opening tag here and I know that inside of the gallery tag there is going to be information about some sort of gallery. At the bottom of the code you can see the closing gallery tag that just looks like a closing HTML tag, which is the same thing as the opening tag except it has a forward slash (/) before the tag name.
In XML tags are also called elements. Inside of the gallery element we have some processing instructions so we have an open bracket, question mark. The processing instructions here, question mark. And then a closing bracket. Then we have an XML comment, which is the same thing as an HTML comment. Inside of the brackets we have an exclamation point (!) Two hyphens, the body of the comment, two hyphens, and then a bracket to close. And then following our hierarchy or XML tree, we have three child elements inside of the gallery element.
So the first child I have highlighted here is an image, the second child is another image and the third child is another image. The first child contains an attribute. An attribute in XML is just like an attribute in HTML. It's information that's contained inside of the opening tag. So we have the tag name, which is image, and space, the attribute name, which is file, an = and then a value in quotes. So this contains information about where the file is for this image. Then we have information on our artist in the artist element.
Information about the title of the image contained in the title element. Then we have the description contained in a description element. The only thing that may look a little bit different inside of the description element is the CDATA tag and that just tells the XML parser to not process anything inside of this tag as XML data. And that's so that we can use reserved XML characters, like the < and >, inside of a body text block for example.
So if we look at our text here, it says here's a picture of some stuff and the picture is surrounded in a b tag and the b tag is an HTML tag that makes content bold. Now if you're familiar with web standards you may we be wondering why I chose not to use a strong tag instead. That's because ActionScript supports a number of HTML tags and it's actually a very small amount and the strong tag is not supported, but the b tag is. So I'm using the b tag here because I want this picture to be bold when I load this XML data into Flash and display it in a text field.
So by putting in a CDATA tag the XML parser doesn't read that b tag as an XML tag. So there's look at our file. Now let's start creating XML using ActionScript.
- 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