Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Flex 3 with Java

You're reading from  Flex 3 with Java

Product type Book
Published in Jun 2009
Publisher Packt
ISBN-13 9781847195340
Pages 304 pages
Edition 1st Edition
Languages

Table of Contents (18) Chapters

Flex 3 with Java
Credits
About the Author
About the Reviewers
Preface
Installing and Configuring Adobe Flex Introduction to Flex 3 Framework Introduction to ActionScript 3.0 Using External API and LocalConnection Working with XML Overview of LiveCycle Data Services and BlazeDS Flex Data Access Methods Communicating with Server-side Java Debugging Techniques Styling your Application Packaging and Deployment Internationalization and Localization Creating an E-commerce Application

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 basics...

XML basics


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:

<book>
<title>Flex 3 with Java</title>
<author>Satish Kore</author>
<publisher>Packt Publishing</publisher>
<pages>300</pages>
</book>

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...

Understanding E4X


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:

private var xmlUrl:String = "http://www.foo.com/rssdata.xml";
private var request:URLRequest = new URLRequest(xmlUrl);
private var loader:URLLoader = new URLLoader(;
private var rssData:XML;
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(request);
private function completeHandler(event:Event):void {
rssData = XML(loader.data);
trace(rssData);
}

Let's see one quick complete sample of loading RSS data from the Internet:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application...

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...

Summary


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.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Flex 3 with Java
Published in: Jun 2009 Publisher: Packt ISBN-13: 9781847195340
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}