Convert a flex array to XML

Source: Internet
Author: User
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "vertical" creationcomplete = "avithandle ()">
<Mx: SCRIPT>
<! [CDATA [
Import MX. rpc. Events. faultevent;
Import MX. rpc. Soap. WebService;
Import MX. rpc. Events. resultevent;
Import MX. Controls. Alert;

Public Function avithandle (): void
{
VaR Web: WebService = new WebService ();
Web. WSDL = "http: // localhost: 2250/flex % 203.0% 20asp.net % 20 test/webservicetest. asmx? WSDL ";
// The loadwsdl method must be called to download the WS description file. Otherwise, the WebService method is not responded even if it is called.
Web. loadwsdl ();
// Added the listener for successful call
Web. addeventlistener (resultevent. Result, wshandle );
// Add an error call listener
Web. addeventlistener (faultevent. Fault, fault );
Web. readxml. Send ();
}

// The processing function is successfully called.
Public Function wshandle (E: resultevent): void
{
// DDD. Text = E. result as string;
VaR menus: xml = XML (E. result as string );
VaR results: xmllist = menus. Children ();

VaR array: array = elementtoattr (results );

Datagrid1.dataprovider = array;
}

/* Convert the following types of XML into an array to bind data to the DataGrid, tree, and other components as dataprovider.
<? XML version = "1.0" encoding = "UTF-8"?>
<Items>
<Item>
<Name> nam1 </Name>
<Grender> boy </grender>
<From> Guangxi </from>
</Item>
<Item>
<Name> nam2 </Name>
<Grender> boy </grender>
<From> Shanghai </from>
</Item>
</Items>
*/
Public Function elementtoattr (results: xmllist): Array
{
VaR array: array = new array ();
For each (VAR child: XML in results ){
VaR OBJ: Object = new object ();

OBJ [Child. nodekind ()] = Child [Child. nodekind ()];
For each (VAR ite: XML in child. Children ()){
OBJ [ite. Name (). tostring ()] = Child [ite. Name (). tostring ()];
}

/*
OBJ ["name"] = Child. Name;
OBJ ["grender"] = Child. grender;
OBJ ["from"] = Child. From;
*/

Array. Push (OBJ );
}
Return
Array;
}

// Error handling function
Public Function fault (E: faultevent): void
{
Alert. Show (E. fault. faultstring, 'error ')
}
]>
</MX: SCRIPT>
<Mx: Label text = "label" id = "DDD" fontsize = "12"/>
<Mx: DataGrid id = "datagrid1">
<Mx: columns>
<Mx: datagridcolumn headertext = "name" datafield = "name"/>
<Mx: datagridcolumn headertext = "gender" datafield = "grender"/>
<Mx: datagridcolumn headertext = "" datafield = "from"/>
</MX: columns>
</MX: DataGrid>
</MX: Application>

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.