5 Replies Latest reply on Aug 12, 2010 9:47 PM by samschauhan

    Reading Webservice Result

    samschauhan

      I am getting a webservice result as:

       

      "<StockQuotes><Stock><Symbol>ABC</Symbol><Last>20.24</Last><Date>8/6/2010</Date><Time>4:00pm</Time><Change>0.00</Change><Open>N/A</Open><High>N/A</H igh><Low>N/A</Low><Volume>0</Volume><MktCap>41.563B</MktCap><PreviousClose>20.24</Previous Close><PercentageChange>0.00%</PercentageChange><AnnRange>14.72 - 20.97</AnnRange><Earns>0.706</Earns><P-E>28.67</P-E><Name>ABC Corporation C</Name></Stock></StockQuotes>"  

       

      This is stored in the event.result property in the ResultHandler .

       

      How can I parse this response and read, say, Last tag?

       

      event.result.Last gives an error.

       

      Thanks

      SAM

        • 1. Re: Reading Webservice Result
          Balakrishnan V Level 3

          Store this event.result into an XML type object.

           

          Ex:

           

          var resultXML:XML=new XML()

          resutlXML = event.result as XML

           

          //To read Last, use

           

          resultXML.Stock.Last

           

          For more details, look up

          http://wso2.org/project/mashup/0.2/docs/e4xquickstart.html (OR)

          http://www.trilemetry.com/clients/f3gse/partIII/partIIIa/workingWithData_e4x.cfm#

           

          [They are simple E4X tutorials]

           

          Hope this helps

           

          Balakrishnan V

          • 2. Re: Reading Webservice Result
            samschauhan Level 1

            Thanks for your reply, but I am afraid it still didnt work.

             

            Here is my complete code:

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                            layout="absolute">
                <mx:WebService id="userRequest"
                               wsdl="http://www.webservicex.net/stockquote.asmx?wsdl"
                               result="resultHandler(event)">
                </mx:WebService>

             

                <mx:Script>
                    <![CDATA[
                        import mx.utils.XMLUtil;
                        import mx.rpc.events.ResultEvent;

             

                        private function resultHandler(event:ResultEvent):void
                        {
                            var resultXML:XML=new XML();

             

                            resultXML=event.result as XML;
                            trace(resultXML.Stock.Last);
                            display = resultXML.Stock.Last;
                        }

             

                        private function clickHandler():void
                        {
                            var stock:String = new String();
                            stock = input.text;
                            userRequest.GetQuote(stock);
                        }
                    ]]>
                </mx:Script>
                <mx:Label id="enter"
                          x="41"
                          y="40"
                          text="Enter Symbol:"
                          width="88"
                          height="25"
                          textAlign="left"/>
                <mx:Button x="325"
                           y="38"
                           label="Get Quote"
                           click="clickHandler()"/>
                <mx:Label id="display"
                          x="41"
                          y="98"
                          text="N/A"
                          width="160"
                          height="28"/>
                <mx:TextInput id="input" x="137" y="38"/>
                </mx:Application>

            • 3. Re: Reading Webservice Result
              Balakrishnan V Level 3

              I'm sorry. You need to give resultXML=new XML(event.result).

               

              Btw, also change the "display" to "display.text"

               

              With this, it works. I tried it out.

               

              Balakrishnan V

              1 person found this helpful
              • 4. Re: Reading Webservice Result
                BhaskerChari Level 4

                Hi,

                 

                Replace your resultHandler() function with the below one...

                 

                private function resultHandler(event:ResultEvent):void
                            {
                                var resultXML:XML=new XML();
                                resultXML = new XML(event.result as String);

                               if(resultXML != null && resultXML.hasOwnProperty("Stock"))

                               {

                                    trace(resultXML.Stock.Last);
                                    display.text = resultXML.Stock.Last;

                               }
                             }

                 

                Thanks,

                Bhasker Chari

                 

                • 5. Re: Reading Webservice Result
                  samschauhan Level 1

                  Bhasker/Bala,

                   

                  Thanks for your quick help. It worked!

                   

                  SAM