Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
822,68 KB
Nội dung
Chapter 24: Managing XML with E4X
771
If you want to identify namespaces by their URI, use the ActionScript’s namespace keyword to
create a namespace object by the URI:
private namespace train = “http://www.bardotech.com/train”;
private namespace plane = «http://www.bardotech.com/airplane»;
private namespace car = “http://www.bardotech.com/automobile”;
Alternatively, if you want to identify namespaces by their prefixes as assigned in the XML struc-
ture, create variables typed as the
Namespace class. Assign each namespace by calling the XML
object’s
namespace() method and passing the selected namespace prefix:
private var train:Namespace = xTravel.namespace(“train”);
private var plane:Namespace = xTravel.namespace(“plane”);
private var car:Namespace = xTravel.namespace(“car”);
Note
The Namespace class is a top-level Flash Player class, meaning that it isn’t a member of any particular pack-
age and can be used without requiring an
import statement.
n
Tip
Notice that the names of the ActionScript namespace objects match the namespace prefixes in the XML con-
tent. This isn’t technically necessary; as long as the namespace Uniform Resource Identifier (URI) or prefix
match, XML elements will be identified correctly. But consistency between data and code notation certainly
doesn’t hurt.
n
After the namespace objects have been declared, you can use them as element and attribute pre-
fixes in E4X expressions. The namespace object’s name is separated from the element or attribute
name with the
:: operator to qualify the node as being a member of the selected namespace.
The following code extracts the
<traveltime> element that’s qualified with the plane
namespace:
traveltime = xTravel.journey.plane::traveltime;
The application in Listing 24.6 declares an XML structure and then enables the user to indicate
which of the three
<traveltime> values she wants to see.
LISTING 24.6
Using XML namespaces in E4X
<?xml version=”1.0” encoding=”utf-8”?>
<s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009”
xmlns:s=”library://ns.adobe.com/flex/spark”
xmlns:mx=”library://ns.adobe.com/flex/mx”>
continued
31_488959-ch24.indd 77131_488959-ch24.indd 771 3/5/10 2:39 PM3/5/10 2:39 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part III: Working with Data
772
LISTING 24.6
(continued)
<s:layout>
<s:VerticalLayout horizontalAlign=”center” paddingTop=”20”/>
</s:layout>
<fx:Style>
@namespace s “library://ns.adobe.com/flex/spark”;
@namespace mx “library://ns.adobe.com/flex/mx”;
s|RadioButton {
font-size:12;
font-weight:bold;
}
</fx:Style>
<fx:Declarations>
<s:RadioButtonGroup id=”vehicleGroup” itemClick=”getTravelTime()”/>
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable]
private var travelTime:String=”Choose a vehicle”;
private var xTravel:XML =
<travel
xmlns:train=”http://www.bardotech.com/train”
xmlns:plane=”http://www.bardotech.com/airplane”
xmlns:car=”http://www.bardotech.com/automobile”>
<journey>
<train:traveltime>8 hours</train:traveltime>
<plane:traveltime>1 hour</plane:traveltime>
<car:traveltime>3 days</car:traveltime>
</journey>
</travel>
private namespace train = “http://www.bardotech.com/train”;
private namespace plane = “http://www.bardotech.com/airplane”;
private namespace car = “http://www.bardotech.com/automobile”;
private function getTravelTime():void
{
var vehicle:String = vehicleGroup.selectedValue as String;
switch (vehicle)
{
case “plane”:
travelTime = xTravel.journey.plane::traveltime;
break;
case “train”:
travelTime = xTravel.journey.train::traveltime;
break;
case “car”:
travelTime = xTravel.journey.car::traveltime;
}
}
31_488959-ch24.indd 77231_488959-ch24.indd 772 3/5/10 2:39 PM3/5/10 2:39 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 24: Managing XML with E4X
773
]]>
</fx:Script>
<s:Panel title=”Select a vehicle” width=”135”>
<s:layout>
<s:VerticalLayout paddingBottom=”5” paddingLeft=”5”
paddingRight=”5” paddingTop=”5”/>
</s:layout>
<s:RadioButton label=”Plane” value=”plane”
groupName=”vehicleGroup”/>
<s:RadioButton label=”Train” value=”train”
groupName=”vehicleGroup”/>
<s:RadioButton label=”Automobile” value=”car”
groupName=”vehicleGroup”/>
<s:controlBarContent>
<s:Label text=”{travelTime}”/>
</s:controlBarContent>
</s:Panel>
</s:Application>
On the Web
The code in Listing 24.6 is available in the Web site files as E4XWithNamespaces.mxml in the default pack-
age in the
src folder of the chapter24 project.
n
Figure 24.2 shows the resulting application, displaying the results of an E4X expression with
namespaces to the user.
FIGURE 24.2
An application using E4X with namespaces
31_488959-ch24.indd 77331_488959-ch24.indd 773 3/5/10 2:39 PM3/5/10 2:39 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part III: Working with Data
774
Summary
In this chapter, I described how to use E4X to parse and modify data stored in XML objects in Flex
application memory at runtime. You learned the following:
l
E4X stands for EcmaScript for XML.
l
E4X is a standard of Ecma International that is implemented in ActionScript 3 and in
certain other languages and platforms.
l
E4X enables you to parse, extract, and modify XML-based data at runtime with simple,
concise expressions.
l
E4X is a part of the compiled ActionScript language and is not designed for runtime
evaluation of arbitrary expressions.
l
Array-style syntax is combined with various operators to “walk” the XML hierarchy.
l
The delete operator removes elements and attributes at runtime.
l
XML with namespaces can be accurately parsed using namespace objects and the
namespace qualification operator (
::).
31_488959-ch24.indd 77431_488959-ch24.indd 774 3/5/10 2:39 PM3/5/10 2:39 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IV
Integrating Flex
Applications with
Application Servers
IN THIS PART
Chapter 25
Working with SOAP-Based
Web Services
Chapter 26
Integrating Flex Applications
with BlazeDS and Java
Chapter 27
Using the Message Service
with BlazeDS
Chapter 28
Integrating Flex Applications
with ColdFusion
Chapter 29
Integrating Flex Applications
with PHP
32_488959-pp04.indd 77532_488959-pp04.indd 775 3/5/10 2:40 PM3/5/10 2:40 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
32_488959-pp04.indd 77632_488959-pp04.indd 776 3/5/10 2:40 PM3/5/10 2:40 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
777
CHAPTER
Working with SOAP-
Based Web Services
IN THIS CHAPTER
Understanding SOAP
Understanding WSDL
Using the
WebService
component
Handling
WebService
component events
Calling Web service operations
and displaying data with Flash
Builder data connections
I
n Chapter 23, I described the use of the Flex HTTPService compo-
nent to make requests and handle responses from Web resources for-
matted as arbitrary XML data structures. The strength of REST
(Representational State Transfer) and generic XML is that you can create and
use Web services that employ any arbitrary data structure. The potential
weakness of this strategy is that each application must have specific knowl-
edge of the particular XML structure being used.
SOAP-based Web services take a different approach: They employ industry-
standard XML languages to format both messages and metadata. The SOAP
language itself is used to format requests and responses between a client and
a server, while WSDL (Web Services Description Language) is used to
declare to Web service consumers the structure and capabilities of Web ser-
vice operations.
Note
The term SOAP started as an acronym for Simple Object Access Protocol.
Starting with version 1.2, it became simply SOAP.
n
The strength of SOAP-based Web services lies in their industry-level stan-
dardization and their capability to accept strongly typed parameters and
return values in a way that RESTful operations typically can’t. Their weak-
ness lies in the verbose nature of the messages that are exchanged between
the client and the server. Because SOAP-based Web services send and receive
data in XML, the message packets are significantly larger than the same data
encoded in AMF (Action Message Format; the binary format used by the
RemoteObject class).
33_488959-ch25.indd 77733_488959-ch25.indd 777 3/5/10 2:40 PM3/5/10 2:40 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IV: Integrating Flex Applications with Application Servers
778
SOAP servers and clients are designed to be interoperable, so that you can easily call functions
(known in SOAP as operations) from objects on remote servers without knowing what platform is
hosting the service or what programming language was used to develop it, because many support
SOAP. And, as data is passed between client and server, its data types are maintained as long as
both tiers of the application use compatible types.
Web Resource
The SOAP and WSDL recommendations are managed by the World Wide Web Consortium (W3C), which also
manages the recommendations for XML, HTML, and HTTP. The most recent recommendations are available at
www.w3.org/TR/soap and www.w3.org/TR/wsdl.
For a history of SOAP, check out Dave Winer’s “Dave’s History of SOAP” at www.xmlrpc.com/stories/
storyReader$555
and Don Box’s “A Brief History of SOAP” at http://webservices.xml.com/
pub/a/ws/2001/04/04/soap.html
.
n
On the Web
To use the sample code for this chapter, import the chapter25.fxp project from the Web site files into any
folder on your disk. The sample Web service files are built in the CFML (ColdFusion Markup Language) pro-
gramming language for use with Adobe ColdFusion and should work with either ColdFusion 8 or 9. You can
download the free developer edition of ColdFusion from
www.adobe.com/products/coldfusion.
n
Understanding SOAP
SOAP is an XML language that’s used to format messages sent between clients and servers in RPC
(Remote Procedure Call)–style applications. Its purpose is to allow client applications to call func-
tions of remote objects that are defined and hosted in a server-based environment.
When a remote operation is called from a SOAP client application, the request message is encoded
in the SOAP language as an XML package with a root element named
<Envelope>. The following
SOAP packet was generated by a Flex application calling a remote operation named
helloWorld:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=”http://schemas.xmlsoap.org/soap/envelope/”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
<SOAP-ENV:Body
SOAP-ENV:encodingStyle=
“http://schemas.xmlsoap.org/soap/encoding/”>
<intf:helloWorld xmlns:intf=”http://flex4bible”/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
When the response comes back from the server, it’s encoded in the same XML language. The
following SOAP response was generated by a Web service written in CFML and hosted by
Adobe ColdFusion:
33_488959-ch25.indd 77833_488959-ch25.indd 778 3/5/10 2:40 PM3/5/10 2:40 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Chapter 25: Working with SOAP-Based Web Services
779
<?xml version=”1.0” encoding=”utf-8”?>
<soapenv:Envelope
xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”
xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
<soapenv:Body>
<ns1:helloWorldResponse
soapenv:encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/”
xmlns:ns1=”http://flex4bible”>
<helloWorldReturn
xsi:type=”xsd:string”>Hello World</helloWorldReturn>
</ns1:helloWorldResponse>
</soapenv:Body>
</soapenv:Envelope>
If you compare the outgoing and incoming SOAP data packets, you’ll see that they use the same
XML namespace,
http://schemas.xmlsoap.org/soap/envelope/, to define the elements
and attributes of the SOAP language. They differ in certain minor details, such as the capitalization
of namespace prefixes (Flex uses
SOAP-ENV, while ColdFusion uses soap-env), but they agree
on the important elements of Web-based communications.
The magic of SOAP, however, is that you don’t need to know these details. SOAP-based client and
server software is responsible for creating an abstraction layer that enables the developer to make calls
to remote operations using code that’s only minimally different from that used to call local methods.
A SOAP-based Web service can be built with many different programming languages and hosted
on many operating systems. To host a service, you need an application server that knows how to
read and write SOAP message packets. Similarly, the client application uses an implementation of
SOAP that handles the serialization and deserialization of the SOAP message packets as data is sent
and received.
Some SOAP-based software packages implement both server and client functionality. For example,
Apache’s Axis (
http://ws.apache.org/axis/) is a popular Java-based implementation of
SOAP that implements client and server functionality and can be used freely with any Java-based
application. Other implementations, such as the Flex SDK’s
WebService component, include
only a SOAP client.
This chapter describes how to use the
WebService component to make calls to SOAP-based Web
services. While the examples in this chapter are written against a Web service built and hosted in
Adobe ColdFusion, Flex applications are interoperable with many SOAP server implementations.
For example:
l
Microsoft ASP.NET implements SOAP as a feature named XML Web Services.
l
Apache Axis includes implementations of SOAP for client-side and server-side Java-based
applications on most operating systems.
l
Adobe ColdFusion (used in this chapter) implements SOAP as an option for calling
ColdFusion Component (CFC) functions and uses the
<cfinvoke> command to call
33_488959-ch25.indd 77933_488959-ch25.indd 779 3/5/10 2:40 PM3/5/10 2:40 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Part IV: Integrating Flex Applications with Application Servers
780
functions from most SOAP servers. The most recent versions, ColdFusion 8 and 9, run on
Windows, Mac OS X, Linux, Solaris, and AIX.
l
Many open-source and built-in implementations of SOAP also are available for various
scripting languages, including PHP, Python, and Ruby.
Note
Whenever possible, most developers prefer to use the RemoteObject component to integrate Flex applica-
tions with ColdFusion, Java-based applications like LiveCycle Data Services and BlazeDS, and other non-Adobe
products, such as OpenAMF, AMFPHP, and WebOrb, that support the Remoting Service architecture and
binary AMF. This is primarily due to the performance advantage you get with AMF.
SOAP, while supporting strongly defined data types, is formatted as XML and generates much larger data pack-
ets than AMF-enabled architectures. Web service integration tends to be used for integration with third-party
data vendors who support the SOAP standard or with application servers with particularly strong SOAP sup-
port, such as ASP.NET.
n
Understanding WSDL
Web Services Description Language (WSDL) is an XML language that’s used to declare to Web ser-
vice consumers the structure and capabilities of Web service operations. In order to consume a
Web service, a Flex application must be able to read and parse a WSDL file at runtime that tells the
WebService component everything it needs to know in order to successfully call the service’s
operations.
WSDL is a somewhat complex language, but many SOAP server implementations, including
Apache Axis, ASP.NET’s XML Web Services, and ColdFusion, can dynamically generate a WSDL
file for a native class exposed as a Web service in response to an HTTP request from a client appli-
cation. For all these application servers, you generate a WSDL file by sending an HTTP request
from a client application to the service URL and appending a query string variable named
wsdl.
Take as an example a ColdFusion Component (CFC) named
SoapService.cfc that’s designed
to be called as a Web service. If the CFC is stored in a subfolder of the Web root named
services,
and ColdFusion is installed on your local server and connected to a Web server running on the
default port 80, the CFC’s URL would be:
http://localhost/services/SoapService.cfc
To generate the WSDL file, append a query string parameter named wsdl:
http://localhost/services/SoapService.cfc?wsdl
ColdFusion responds by generating the WSDL content and returning it to the requesting applica-
tion. Similar patterns are used by other common SOAP server applications. This is an example of a
WSDL URI for Apache Axis:
http://localhost/myJEEApp/services/MyWebService?wsdl
33_488959-ch25.indd 78033_488959-ch25.indd 780 3/5/10 2:40 PM3/5/10 2:40 PM
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
[...]... server’s Web root named flex4 bible The default Web root folder in this environment is C:\ColdFusion9\wwwroot on Windows and /Applications/ColdFusion9/wwwroot on Mac OS X 2 Locate the files and directories in the ColdFusionFiles folder within the chapter25 project 3 Copy and paste the files into the new flex4 bible folder under the ColdFusion Web root You should have these files and directories: l Application.cfc... operations directly from the Web service As a result, FlashBuilderand the Flex compiler can do a better job with code completion and compile-time syntax checking l Local proxy methods are structured with required arguments As a result, you get better code hints and completion as you write the code Once you’ve created the data connection, FlashBuilder also provides tools to return data as automatically... Services And this is an example for ASP.NET: http://localhost/myDotNetApp/MyWebService.asmx?wsdl Note The address of the WSDL document on the Web is referred to in Flash Builder and the Flex documentation as the WSDL URI (Uniform Resource Identifier) n Web Resource The WSDL language is managed by the W3C The current recommendation is available at www.w3.org/TR/ wsdl n WSDL is standardized across vendors and. .. object in MXML, declare it with a unique id and set its wsdl property as in this example: New Feature In Flex4 applications, the tag must be placed within the element n Tip As with the HTTPService component, if a Web-based Flex application and a Web service it calls are hosted in... myService:WebService = new WebService( “http://localhost:8500 /flex4 bible/SoapService.cfc?wsdl”); myService.loadWSDL(); Another approach is to pass the wsdl location into loadWSDL() and handle both tasks at the same time: 7 84 Chapter 25: Working with SOAP-Based Web Services var myService:WebService = new WebService(); myService.loadWSDL( “http://localhost:8500 /flex4 bible/SoapService.cfc?wsdl”); Invoking an operation... IV: Integrating Flex Applications with Application Servers Handling Web service results As with the HTTPService component, Web service requests and responses are handled asynchronously This means that when you send a request, Flash Player’s ActionScript Virtual Machine (AVM) doesn’t pause in its code execution and wait for data to be returned Instead, you call the Web service operation and then use either... wsdl=”http://localhost:8500 /flex4 bible/SoapService.cfc?wsdl” fault=”faultHandler(event)”> The application in Listing 25 .4 declares MXML-based result event handlers for each of two Web service operations The fault event handler is declared in the tag and is used by both of the... Web Services LISTING 25 .4 Handling events with multiple Web service operations . results of an E4X expression with
namespaces to the user.
FIGURE 24. 2
An application using E4X with namespaces
31 _48 8959-ch 24. indd 77331 _48 8959-ch 24. indd 773.
E4X stands for EcmaScript for XML.
l
E4X is a standard of Ecma International that is implemented in ActionScript 3 and in
certain other languages and