سؤال

I have done my EmployeeDetails application using HTTPService. But I want to load my XML file programmatically. I tried it using URLLoader and URLRequest. But no luck. I couldn't done it.

Sample code using HTTPService:

    <fx:Declarations>   
    <s:XMLListCollection id="employeeXMLList" filterFunction="xmlListCollectionFilterFun"/>
    <s:HTTPService id="employeeService" url="http://localhost/demo/TextXmlFile.xml" 
                   method="POST" result="employeeService_resultHandler(event)"
                   fault="employeeService_faultHandler(event)"
                   resultFormat="xml"/>
</fx:Declarations>
<fx:Script>
    <![CDATA[
        protected function employeeService_resultHandler(event:ResultEvent):void
        {
            var xmlList:XMLList = XML(event.result).Employee;
            employeeXMLList = new XMLListCollection(xmlList);
        }

        protected function employeeService_faultHandler(event:FaultEvent):void
        {
            spark.components.Alert.show(event.fault.faultString,"Fault Error Message");
        } 
    ]]>
</fx:Script>

It's working well. Now I change this as follows:

<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"
           xmlns:components="components.*"
           minWidth="955" minHeight="600"
           creationComplete="load()">    
   <fx:Script>
    <![CDATA[
            private var request:URLRequest = new URLRequest("http://localhost/demo/TextXmlFile.xml");
        //request.contentType = "XML";
        //request.method = URLRequestMethod.POST;
        private var xmlData:XML;
        private var loader:URLLoader = new URLLoader();

        private function loadXML(event:Event):void
        {
            xmlData = new XML(event.target.date);               
        }

        private function load():void
        {
            loader.addEventListener(Event.COMPLETE, loadXML);
            loader.load(request);
        }

    ]]>
</fx:Script>

And I don't know what I am done here. Anyone can tell me the correct way to do this? And Please give me some explanation for that?

My XML:

<?xml version="1.0" encoding="UTF-8"?>
<CompanyEmployees version="1">
    <Employee>
        <Name>John</Name>
        <Id>234</Id>
        <DOB>1990/04/02</DOB>
        <Designation>Project manager</Designation>
        <Department>Mobile</Department>
        <DOJ>2008/04/11</DOJ>
        <Experience>15</Experience>
        <Mobile>9999999999</Mobile>
        <Email>john.a@Sybrant.com</Email>
    </Employee>
    <Employee>
        <Name>Adya</Name>
        <Id>135</Id>
        <DOB>1989/04/21</DOB>
        <Designation>Software Engineer</Designation>
        <Department>Flex</Department>
        <DOJ>2009/05/15</DOJ>
        <Experience>5</Experience>
        <Mobile>76890678990</Mobile>
        <Email>adya@Sybrant.com</Email>
    </Employee>

</CompanyEmployees>

UPDATE

            private var urlRequest:URLRequest;
        urlRequest = new URLRequest("http://localhost/TextXmlFile.xml");
        urlRequest.contentType = "XML";
        urlRequest.method = URLRequestMethod.POST;

I am getting error like "Access of undefined property urlRequest."

هل كانت مفيدة؟

المحلول

It seems to be typo error.

private var request:URLRequest;
private var xmlData:XML;
private var loader:URLLoader = new URLLoader();

private function loadXML(event:Event):void
{
    xmlData = new XML(event.target.data);     //Note here data instead of date.          
}

private function load():void
{
    request = new URLRequest("http://localhost/demo/TextXmlFile.xml");
    request.contentType = "XML";
    request.method = URLRequestMethod.POST;
    loader.addEventListener(Event.COMPLETE, loadXML);
    loader.load(request);
}

Reason :

You can only do declare and initialize/instantiate object out side of functions.

Normally compiler expect out side of function should be variable declaration(protected/public/private). So we can't assign value of those at out of function like

request.contentType = "XML";
request.method = URLRequestMethod.POST;

This is exact place where function comes in. Sometime it is possible with static block if all neccessary function and variable should be static.

More details about AS3 Static Block https://www.google.co.in/search?q=static+block+in+as3

or Check out SO Can we use static initializers in a Flex Library?

Check docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLLoader.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top