Chapter 5. Working with XML
In today's world, many server-side applications make use of XML to structure data because XML is a standard way of representing structured information. It is easy to work with, and people can easily read, write, and understand XML without the need of any specialized skills. The XML standard is widely accepted and used in server communications such as Simple Object Access Protocol (SOAP) based web services. XML stands for eXtensible Markup Language. The XML standard specification is available at http://www.w3.org/XML/.
Adobe Flex provides a standardized ECMAScript-based set of API classes and functionality for working with XML data. This collection of classes and functionality provided by Flex are known as E4X. You can use these classes provided by Flex to build sophisticated Rich Internet applications using XML data.
This chapter covers the E4X approach to process XML data with a comprehensive example application using these techniques to process XML data.
XML is a standard way to represent categorized data into a tree structure similar to HTML documents. XML is written in plain-text format, and hence it is very easy to read, write, and manipulate its data.
A typical XML document looks like this:
Generally, XML data is known as XML documents and it is represented by tags wrapped in angle brackets (<>
). These tags are also known as XML elements. Every XML document starts with a single top-level element known as the root element. Each element is distinguished by a set of tags known as the opening tag and the closing tag. In the previous XML document,<book>
is the opening tag and</book>
is the closing tag. If an element contains no content, it can be written as an empty statement (also called self-closing statement). For example...
Flex provides a set of API classes and functionality based on the ECMAScript for XML (E4X) standards in order to work with XML data. The E4X approach provides a simple and straightforward way to work with XML structured data, and it also reduces the complexity of parsing XML documents.
Earlier versions of Flex did not have a direct way of working with XML data. The E4X provides an alternative to DOM (Document Object Model) interface that uses a simpler syntax for reading and querying XML documents. More information about other E4X implementations can be found at http://en.wikipedia.org/wiki/E4X.
The key features of E4X include:
It is based on standard scripting language specifications known as ECMAScript for XML. Flex implements these specifications in the form of API classes and functionality for simplifying the XML data processing.
It provides easy and well-known operators, such as the dot (.) and @
, to work with XML objects.
The @
and dot (.) operators can be used not...
Loading external XML documents
You can use the URLLoader
class to load external data from a URL. The URLLoader
class downloads data from a URL as text or binary data. In this section, we will see how to use the URLLoader
class for loading external XML data into your application. You can create a URLLoader
class instance and call the load()
method by passing URLRequest
as a parameter and register for its complete event to handle loaded data. The following code snippet shows how exactly this works:
Let's see one quick complete sample of loading RSS data from the Internet:
An example: Building a book explorer
By this time, you would be comfortable in writing Flex applications by using many features of Flex and ActionScript, which you have learned in the previous chapters. In this section, we will build something more complicated and interesting by using many features, including custom components, events, data binding, E4X, loading external XML data, and so on.
We will build a sample books explorer, which will load a books catalog from an external XML file and allow the users to explore and view details of books. We will also build a simple shopping cart component, which will list books that a user would add to cart by clicking on the add to cart button.
Create a new Flex project using Flex Builder. Once the project is created, create an \assets\images\
folder under its src
folder. This folder will be used to store images used in this application. Now start creating the following source files into its source folder.
Let's start by creating a simple book catalog...
In this chapter, you learned what E4X is and how to use it to work with XML data. You also learned various Flex classes to work with XML data, and how to load external XML files and use XML as a data provider. You also created a sample books explorer application using various concepts such as custom component, event creation, data binding, and E4X.
In the next chapter, you will learn about Adobe's LiveCycle Data Services and what is the BlazeDS platform.