Parsing XML has never been fun. Java programmers use way too many different parsers and APIs.
Java 6 includes Java Architecture for XML Binding (JAXB 2.0) and the implementation of the XML
Data Binding Specification (JSR 31) that maps JavaBeans and XML data. To be more accurate, JAXB
2.0 offers two-directional mapping between JavaBeans and XML Schema (see the javax.xml.bind
package). Java 6 comes with a new tool called xjc that takes an XML Schema as an input and generates
the required JavaBeans as an output.
ActionScript 3.0 supports E4X, which is an ECMA standard for working with XML (see http://www.
ecma-international.org/publications/files/ECMA-ST/ECMA-357.pdf ). It’s very powerful and yet
simple to use. You can forget about these SAX and DOM parsers. E4X is a step towards making XML
a programming language.
For example, an MXML program can read the XML file people.xml (or any other XML source) shown
in Listing 4.23 into a variable with only one line (without worrying about error processing):
<mx:XML id=”myXmlFile” source=”people.xml”/>
You’ll need another line of code to populate the data grid using Flex data binding (remember, tying
the data from a source to a destination). In our case myXmlFile is the source that populates the data
grid aka destination:
<mx:DataGrid dataProvider=”{myXmlFile.person}”>
This line means that each element <person> will populate one row in the data grid. Let’s make the
XML from Listing 4.23 a bit more complex: we’ll introduce nesting in the name element. Now it
consists of separate <first> and <last> elements.
<?xml version=”1.0” encoding=”UTF-8”?>
<people>
<person>
<name>
<first>Yakov</first>
<last>Fain</last>
</name>
<age>22</age>
<skills>java, HTML, SQL</skills>
CHAPTER 4
RIA WITH ADOBE FLEX AND JAVA 123
</person>
<person>
<name>
<first>Victor</first>
<last>Rasputnis</last>
</name>
<age>21</age>
<skills>PowerScript, JavaScript, ActionScript</skills>
</person>
<person>
<name>
<first>Anatole</first>
<last>Tartakovsky</last>
</name>
<age>20</age>
<skills>SQL, C++, Java</skills>
</person>
</people>
Listing 4.32 The XML file FarataSystems_skills.xml
Our goal is to produce a window that looks like this:
Figure 4.9 A data grid populated from people.xml
Please note that we also want to concatenate the values from the <first> and <last> XML elements
for the data grid’s Name column. A small program in Listing 4.30 does exactly this. The fullName
method concatenates the first and last names, and since we specified the labelFunction property
in the name column, the data rendering will be controlled by the fullName() function. We’ll return
to labelFunction in Chapter 11.
<?xml version=”1.0” encoding=”utf-8”?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”>
Learning Flex Through Applications
124 RIA WITH ADOBE FLEX AND JAVA
<mx:XML id=”myXmlFile” source=”FarataSystems_Skills.xml”/>
<mx:Label text=”Java Developers” fontSize=”18”/>
<mx:DataGrid dataProvider=”{myXmlFile.person}” width=”500”>
<mx:columns>
<mx:DataGridColumn dataField=”name” headerText=”Name” labelFunction=”fullName”/>
<mx:DataGridColumn dataField=”age” headerText=”Age”/>
<mx:DataGridColumn dataField=”skills” headerText=”Skills”/>
</mx:columns>
</mx:DataGrid>
<mx:Script>
<![CDATA[
private function fullName(item:Object, column:DataGridColumn):String {
return item.name.first + “ “ + item.name.last;
}
]]>
</mx:Script>
</mx:Application>
Listing 4.33 Populating a data grid from an XML file
The next step is to add regular expressions to filter the data while populating the data grid. There’s
a nice example of predicate filtering with E4X and RegExp in Darron Schall’s blog at http://www.
darronschall.com/weblog/archives/000214.cfm.
Let’s imagine that a recruiter wants to do a quick search in our XML file to identify people with Java
skills. A small one-line change will do this trick, or at least will be a step in the right direction.
The RegExp class lets you create an instance of the object per the specified pattern, and then find
the substring(s) with this pattern and perform the manipulations with the found substrings, if
any.
In AS3, you can create an instance of the RegExp by using a familiar syntax with a constructor. For
example:
var javaPattern:RegExp = new RegExp(“Java”, “i”);
This is a pattern for finding occurrences of “Java,” ignoring the letter case.
Here’s another way of creating this instance:
var javaPattern:RegExp = /Java/i;
We’ll use the latter syntax by feeding the E4X output to this RegExp instance and the result will be
used as a data provider for the data grid. Let’s modify the MXML code for the <mx:DataGrid> tag to
CHAPTER 4
RIA WITH ADOBE FLEX AND JAVA 125
apply this regular expression to the XML element called skills:
<mx:DataGrid dataProvider=”{myXmlFile.person.(/Java/.test( skills ))}” >
In the line above, /Java/ creates an instance of the RegEx object and the test(skills) method will
ensure that only those XML elements that contain Java are included in the myXmlFile. Now the
resulting window will look as follows:
Figure 4.10 Displaying FarataSystems_skills.xml with Java skills
We still don’t like a couple of things here. First, this output didn’t include Yakov Fain because the
word Java was written in lower case in his skills element. Adding the ignore case option “i” to our
RegExp instance will help:
<mx:DataGrid dataProvider=”{myXmlFile.person.(/Java/i.test(skills))}” >
The output will again look like Figure 4.9.
The next step is to filter out people who were included in this list just because of JavaScript, which
has very little to do with Java. One of the ways to do this is by requesting that there should be a
space or a comma in the regular expression after the word Java:
<mx:DataGrid
dataProvider=”{myXmlFile.person.(/Java? ?,/i.test(skills))}” >
Now we’ve lost both Victor and Anatole. Even though Anatole knows Java, there’s no space or comma
after the word Java in his list of skills. Adding an OR (|) condition that means we’re also interested
in people with the word Java as the last word in the string will help.
<mx:DataGrid
dataProvider=”{myXmlFile.person.(/Java? ?, | Java$/i.test(skills))}”
Learning Flex Through Applications
126 RIA WITH ADOBE FLEX AND JAVA
Figure 4.11 Finding Java programmers
Today, E4X doesn’t support XML Schema, and all the XML elements are returned as text, but it’ll
change in future versions of the ECMAScript for XML. Meanwhile, the implementation of E4X by
any programming language makes it more appealing to developers who have to deal with XML.
Data Binding, and Regular Expressions
Category: Flex Error, Learn Flex | 11 views
0 responses so far!
-
There are no comments yet...Kick things off by filling out the form below.



