Namespaces


E4X has the added capability of XML namespaces. Namespaces are used to avoid element name conflicts. Namespaces are most commonly found in complex XML documents that contain XML data from multiple sources. SOAP, the XML format behind many Web services, is one of the most common examples of XML documents that use namespaces. Many developers never use namespaces in their XML documents, but if you do, there are some new ways to work with them in ActionScript 3.0.

We'll use a simple SOAP envelope to show how you can work with namespaces. The following code shows how to create an XML namespace using literals:

var envelope:XML = <soap:envelope xmlns:soap="http://www.w3.org/2003/05/ soap-envelope" />;


Next, we'll add a body element to the envelope. The body should have the same namespace as the envelope. We can do this a few different ways. We can use the literal syntax to create the body element, as in this example:

envelope.body = <soap:body xmlns:soap="http://www.w3.org/2003/05/soap-envelope" />


We can also create the body element by declaring the namespace and using the :: operator, like this:

var soap:Namespace = new Namespace("http://www.w3.org/2003/05/soap-envelope"); envelope.soap::body = new XML();


Both of these examples create an XML object that looks like the following:

<soap:envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">  <soap:body /> </soap:envelope>


We can also create an XML object using a default namespace, like this:

default xml namespace = new Namespace("http://www.w3.org/2003/05/soap-envelope"); var envelope:XML = <envelope><body /></envelope>;





Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net