Convert XML in Web service to AS3 strongly typed object

Source: Internet
Author: User
Tags soap

SOAP is based on XML and is not easily accessible (using the e4x exception), how do you convert an XML object or XMLList object in a Web service to a AS3 strongly typed object or collection of objects? There are two more common solutions: one is to use the Schematyperegistry.registerclass () method to register a class by using a qualified namespace. The second is to use the Xmldecoder class to convert XML to a AS3 object.

Using the Schematyperegistry.registerclass () method

The Schematyperegistry.registerclass () method allows you to register the class, which is based on the type returned by the Web service. This class must be described in the WSDL file, which describes all the methods and types of the Web service. For example, a corresponding schema fragment named the Person object is as follows:

  code is as follows copy code

< ComplexType name= "Person" >
  <s:sequence>
    <s:element name= "name" type= "s: String "maxoccurs=" 1 "minoccurs=" 0 "/>
    <s:element name=" age "type=" S:unsignedint " maxoccurs= "1" minoccurs= "1"/>
    <s:element name= "married" type= "S:boolean" maxoccurs= "1" minoccurs= "1"/>
  </s:sequence>
</s:complextype>

Note: The type of the service is defined as Tns:person, which indicates that the service will return the person object defined by the preceding code. Schematyperegistry is used to declare mappings, which converts the person object of a Web service to a AS3 strongly typed object. This method requires a namespace-qualified object (Qnmae) and a class object that represents the person returned by the Web service. AS3

The code is as follows Copy Code

var qname:qname = new QName ("http://www.111cn.net/", "person");
Mx.rpc.xml.SchemaTypeRegistry.getInstance (). registerclass (QName, person);

The person class generates a simple value object with public properties that represents the data needed to represent the person object.

The code is as follows Copy Code
Package Com.riafan.model
{
public class Person
{
public Var name:string;
public Var age:uint;
public Var Married:boolean;
}
}

After registering the type, the object returned by the Web service will be converted to the person type, and you can work with this strongly typed object without the AMF service. This method requires a schema for strongly typed objects, so it is not very generic. In addition, it is time-consuming to find schemas using soap, so this method is not efficient.

Using the Xmldecoder.decode () method
The method accesses the XML (not just SAOP) using e4x, and then converts it to a AS3 strongly typed object. So it works better than the previous method. The Xmldecoder class does not exist in the Flex-band API, and if you have a flex Bulder installed, you can eclipsepluginscom.adobe.flexbuilder.project_ The class is found in the XXXDCRADSWCSXXXLIBSSERIALIZERS.SWC. Of course, you can also find them in the source packages that I share.

The first parameter of the decode () method is the XML object to parse, so the result data type returned by the RPC service is typically e4x and the second parameter is the strong type of the corresponding AS3. The third parameter finds the XPath you want to use, which can be seen as efficient. You need to set the fourth argument to true when you need to convert the xmllist to the arraycollection of a strongly typed object. The last parameter specifies whether the AS3 object can be bound.

The code is as follows Copy Code
var ac:arraycollection = Xmldecoder.decode (event.result[0] as XML, person,
"Getlistresponse/getlistresult/person", true, true) as arraycollection;
Dg.dataprovider = AC;

Note: This scenario is directly parsing the XML and cannot directly handle the date data type because it is considered a string type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.