Video Transcript
In this chapter, we are going to learn about how to connect a Flex application to an external data source. There is multiple different ways of doing that in Flex, for example, we can consume web services, we can even call a remote method, for example, a Java object that returns data or we can simply just consume XML. In this chapter, we are going to learn how to actually consume XML and most types of Middleware, for example, PHP, Cold Fusion, ASP, JSP, all of those types of Middleware can actually access the database and dynamically generate XML.
So XML is a great way to actually make our application more dynamic and to connect our application to a server. So for our purposes, we are just going to use the static XML file, but as we are going through this unit, bear in mind that of course we could be using dynamically generated XML from a server. So the first thing I would like you to do is, just look at the static XML that we are going to be using. So just click File, and choose Open File, and go to your Desktop, and then go to your Project files here. Choose hsRPC, choose studentFiles, choose hsRPC, scroll down to where it says Assets and then choose the sandwichData.xml file here.
And inside of this file, you should see XML data and we are going to actually populate controls in our Flex application from this XML data. So notice at the top level you have your root level node called sandwichMenu and then you have a repeating node here called sandwich, which contains information about the different sandwiches that we will be displaying in our Flex application. So for example, the first node we have a sandwich called Dagwood, the second node we have a sandwich called Crooks, the third node we have a sandwich called Mountain, and so on down the line as well as some other information about each of those sandwiches.
Okay, let's get to it and actually start consuming that XML data in our Flex application. So of course our first step is to actually setup a project, so just right click in your Navigator view or Ctrl+Click and choose New, and then choose Flex Project, and lets give our Flex project a name of just xml_data, and I would like you to point to your Student files here which should be, of course, on you desktop, and point to the hsRPC folder that you see here and just click Choose. And this is going to be a web application, make sure we are not using an application server, and click Next.
We want our swf file to be outputted to the Bin folder, click Next again, and just delete the source folder that you see here. For your main application file, I want you to point this to hsRPC_wt1.mxml, and let's just examine what we have here when we click Finish. What you should see, is you should see just a very basic Application tag here as we take a look. And again, it's set to vertical, and again just saves us a little bit of typing there. So the first thing I want us to do is just actually understand how to consume that XML data. So we use HTTP service tag to do that, so we just in mx:HTTPService and lets give this an id of sanData, and lets point the URL to our actual XML file that we just looked at.
So of course, normally this would be a dynamically generated URL, but in this case I am going to just say url=assets, and then I am going to say sandwichData.xml as you see there, and then I will just close off my HTTPService tag. This is going to, of course, return not just the string anymore, like we have been working with up until this point, but this is going to return a complex data structure. So the really nice thing about the Flex framework is it automatically converts that XML data into an array collection.
So this can be a little processor intensive but its very powerful because an array collection contains all the hooks for Data Binding, contains all kinds of methods for us to easily access that data, very, very powerful, and the Flex framework does that automatically. Later on we are also going to learn that the Flex framework has a capability of dealing with XML directly as a native data structure, using the syntax called
In this chapter, we are going to learn about how to connect a Flex application to an external data source. There is multiple different ways of doing that in Flex, for example, we can consume web services, we can even call a remote method, for example, a Java object that...
click to read more