8 Replies Latest reply on May 9, 2009 5:26 PM by 3rdshiftcoder

    webservices - filtering xml to get at data

    3rdshiftcoder

      hi-

       

      i posted earlier. my question may have been confusing to read or follow.

      here is what i ended up with.

       

      This code retrieves weather data - 5 day forecast for a given zip code.

      I was able to get at the data and print it out.

      I think it works now. I have to add on more features.

       

      I dont know how this could be accomplished simpler. I didnt think i'd have to resort to

      regExp code but i could think of no other way.

       

      This was a lot less work than the last time i tried it using php code. Flex and Actionscript 3

      made this easier for me.

       

      Thanks for helping,

      3rdshiftcoder

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute">
      <!--creationComplete="userRequest.GetWeatherByZipCode()"-->
      <mx:WebService id="userRequest"
              wsdl="http://www.webservicex.net/WeatherForecast.asmx?wsdl"
              useProxy="false">
              <mx:operation name="GetWeatherByZipCode" resultFormat="e4x"
              fault="mx.controls.Alert.show(event.fault.faultString)"
              result="remotingCFCHandler(event)">
                  <mx:request>
                      <ZipCode>85044</ZipCode>
                  </mx:request>
              </mx:operation>
          </mx:WebService>
      <mx:Form x="22" y="10" width="493">
      <mx:HBox>
      <mx:Label text="zipcode"/>
      <mx:TextInput id="zipcode" width="300" height="500" />

       

      </mx:HBox>
      <mx:Button label="Submit" click="clickHandler()"/>
      </mx:Form>

         
      <mx:Script>
      <![CDATA[
      import flash.accessibility.AccessibilityImplementation;
      import mx.rpc.events.ResultEvent;

                 
      private function remotingCFCHandler(e:ResultEvent):void
      {

       

      var mystr:String = e.result.toString();

       

      //use regexp pattern match on string to get at the nodes i want.
      var pattern:RegExp = /<MaxTemperatureF>(\d+)<\/MaxTemperatureF>/g;
      var myarr:Array = mystr.match(pattern);
      var superStr:String = '';
      var i:int;
      for (i = 0; i < 5; i++)
      {
         
          superStr = superStr + '   ' + myarr.pop() + "\n";
      }

       

      // put it reassemble back in xml format to get the xml object i wanted in the first place
      superStr = "<MaxTemp>" +"\n" + superStr  +"</MaxTemp>";
      //zipcode.text = superStr;

       

      // now i create a xmllist by filtering the xml object

       

      var x1:XML;
      var x2:XMLList;

       

      x1 = new XML(superStr);
      x1.ignoreWhite = true;
      x2 = x1..MaxTemperatureF;
      zipcode.text = "Phoenix, AZ 5 day forecast" + "\n" +
      "max temperatures" + "\n" + x2.text().toXMLString();

       

      // finally i selected out the node data

       

      }
      private function clickHandler():void
      {
         userRequest.GetWeatherByZipCode();
      }
      ]]>
      </mx:Script>
      </mx:Application>

        • 1. Re: webservices - filtering xml to get at data
          Michael Borbor Level 4

          Could I suggest you to edit your post and put your code inside the Syntax Highlighting - Java so It'll be easier to read.

          • 2. Re: webservices - filtering xml to get at data
            3rdshiftcoder Level 1

            Hi Michael-

             

            I hope this is better. i indented with the space bar. I am using FlashDevelop and not a Java ide.

             

            thanks for the tip.

            3rdshiftcoder

             

            <?xml version= "1.0" encoding= "utf-8"?>
            <mx:Application xmlns:mx= "http://www.adobe.com/2006/mxml" xmlns= "*" layout= "absolute">
            <!--creationComplete="userRequest.GetWeatherByZipCode()"-->
                <mx:WebService id= "userRequest"
                    wsdl="http://www.webservicex.net/WeatherForecast.asmx?wsdl"
                    useProxy="false">
                    <mx:operation name= "GetWeatherByZipCode" resultFormat= "e4x"
                        fault= "mx.controls.Alert.show(event.fault.faultString)"
                        result= "remotingCFCHandler(event)">
                        <mx:request>
                            <ZipCode>85044</ZipCode>
                        </mx:request>
                    </mx:operation>
                </mx:WebService>
            <mx:Form x= "22" y= "10" width= "493">
                <mx:HBox>
                    <mx:Label text= "zipcode"/>
                        <mx:TextInput id= "zipcode" width= "300" height= "500" />
                </mx:HBox>
                <mx:Button label= "Submit" click= "clickHandler()"/>
            </mx:Form>
               
            <mx:Script>
                <![CDATA[
                import flash.accessibility.AccessibilityImplementation;
                import mx.rpc.events.ResultEvent;
               
                private function remotingCFCHandler(e:ResultEvent):void {

             

                    var mystr:String = e.result.toString();

             

                    //use regexp pattern match on string to get at the nodes i want.
                    var pattern:RegExp = /<MaxTemperatureF>(\d+)<\/MaxTemperatureF>/g;
                    var myarr:Array = mystr.match(pattern);
                    var superStr:String = '';
                    var i:int;
                    for (i = 0; i < 5; i++){
                        superStr = superStr + '   ' + myarr.pop() + "\n";
                    }

             

                    // put it reassemble back in xml format to get the xml object i wanted in the first place
                    superStr = "<MaxTemp>" +"\n" + superStr  +"</MaxTemp>";
                    //zipcode.text = superStr;

             

                    // now i create a xmllist by filtering the xml object

             

                    var x1:XML;
                    var x2:XMLList;

             

                    x1 = new XML(superStr);
                    x1.ignoreWhite = true;
                    x2 = x1..MaxTemperatureF;
                    zipcode.text = "Phoenix, AZ 5 day forecast" + "\n" +
                    "max temperatures" + "\n" + x2.text().toXMLString();

             

                    // finally i selected out the node numeric values

             

                    }
                    private function clickHandler():void {
                    userRequest.GetWeatherByZipCode();
                }
            ]]>
            </mx:Script>

             

            </mx:Application>

            • 3. Re: webservices - filtering xml to get at data
              Michael Borbor Level 4

              Well what I meant is that you before posting the code in the forum "post editor" there is this icon >> you click on it and you'll find The Syntax Highlighting-Java.

               

               

              This is the result of that.

              private function clickHandler():void {
                      userRequest.GetWeatherByZipCode();
                  }
              

              Could you post the XML file, and tell me what is your problem fellow developer?

              1 person found this helpful
              • 4. Re: webservices - filtering xml to get at data
                Michael Borbor Level 4

                I see you're using RegEx to construct an XML object. Post the XML please

                1 person found this helpful
                • 5. Re: webservices - filtering xml to get at data
                  3rdshiftcoder Level 1
                  <?xml version= "1.0" encoding= "utf-8"?>
                  <mx:Application xmlns:mx= "http://www.adobe.com/2006/mxml" xmlns= "*" layout= "absolute">
                  <!--creationComplete="userRequest.GetWeatherByZipCode()"-->
                      <mx:WebService id= "userRequest" 
                          wsdl="http://www.webservicex.net/WeatherForecast.asmx?wsdl"
                          useProxy="false">
                          <mx:operation name= "GetWeatherByZipCode" resultFormat= "e4x"
                              fault= "mx.controls.Alert.show(event.fault.faultString)"
                              result= "remotingCFCHandler(event)">
                              <mx:request>
                                  <ZipCode>85044</ZipCode>
                              </mx:request>
                          </mx:operation>
                      </mx:WebService>
                  <mx:Form x= "22" y= "10" width= "493">
                      <mx:HBox>
                          <mx:Label text= "zipcode"/>
                              <mx:TextInput id= "zipcode" width= "300" height= "500" />
                      </mx:HBox>
                      <mx:Button label= "Submit" click= "clickHandler()"/>
                  </mx:Form>
                      
                  <mx:Script>
                      <![CDATA[
                      import flash.accessibility.AccessibilityImplementation;
                      import mx.rpc.events.ResultEvent;
                      
                      private function remotingCFCHandler(e:ResultEvent):void {
                  
                          var mystr:String = e.result.toString();
                  
                          //use regexp pattern match on string to get at the nodes i want.
                          var pattern:RegExp = /<MaxTemperatureF>(\d+)<\/MaxTemperatureF>/g;
                          var myarr:Array = mystr.match(pattern);
                          var superStr:String = '';
                          var i:int;
                          for (i = 0; i < 5; i++){
                              superStr = superStr + '   ' + myarr.pop() + "\n";
                          }
                  
                          // put it reassemble back in xml format to get the xml object i wanted in the first place
                          superStr = "<MaxTemp>" +"\n" + superStr  +"</MaxTemp>";
                          //zipcode.text = superStr;
                  
                          // now i create a xmllist by filtering the xml object
                  
                          var x1:XML;
                          var x2:XMLList;
                  
                          x1 = new XML(superStr);
                          x1.ignoreWhite = true;
                          x2 = x1..MaxTemperatureF;
                          zipcode.text = "Phoenix, AZ 5 day forecast" + "\n" +
                          "max temperatures" + "\n" + x2.text().toXMLString();
                  
                          // finally i selected out the node numeric values
                  
                      }
                      
                       private function clickHandler():void {
                          userRequest.GetWeatherByZipCode();
                      }
                  ]]>
                  </mx:Script>
                  </mx:Application>
                  
                  
                  
                  

                        Hi- Michael.

                               There is no problem anymore ( i hope ). I was just trying to share some code i fixed that was a problem this morning

                               in an earlier post.

                               Thanks for the tip on the indenting. I see i can use xml indent too. very cool.

                               have a good weekend,

                                       jim

                  • 6. Re: webservices - filtering xml to get at data
                    Michael Borbor Level 4

                    You're welcome, have a good weekend too. Please don't forget to close the thread.

                    • 7. Re: webservices - filtering xml to get at data
                      3rdshiftcoder Level 1
                      <GetWeatherByZipCodeResponse xmlns="http://www.webservicex.net" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <GetWeatherByZipCodeResult>
                          <Latitude>33.3291245</Latitude>
                          <Longitude>111.9943</Longitude>
                          <AllocationFactor>0.008745</AllocationFactor>
                          <FipsCode>04</FipsCode>
                          <PlaceName>PHOENIX</PlaceName>
                          <StateCode>AZ</StateCode>
                          <Details>
                            <WeatherData>
                              <Day>Saturday, May 09, 2009</Day>
                              <WeatherImage>http://forecast.weather.gov/images/wtf/nskc.jpg</WeatherImage>
                              <MaxTemperatureF>102</MaxTemperatureF>
                              <MinTemperatureF>67</MinTemperatureF>
                              <MaxTemperatureC>39</MaxTemperatureC>
                              <MinTemperatureC>19</MinTemperatureC>
                            </WeatherData>
                            <WeatherData>
                              <Day>Sunday, May 10, 2009</Day>
                              <WeatherImage>http://forecast.weather.gov/images/wtf/skc.jpg</WeatherImage>
                              <MaxTemperatureF>102</MaxTemperatureF>
                              <MinTemperatureF>67</MinTemperatureF>
                              <MaxTemperatureC>39</MaxTemperatureC>
                              <MinTemperatureC>19</MinTemperatureC>
                            </WeatherData>
                            <WeatherData>
                              <Day>Monday, May 11, 2009</Day>
                              <WeatherImage>http://forecast.weather.gov/images/wtf/few.jpg</WeatherImage>
                              <MaxTemperatureF>102</MaxTemperatureF>
                              <MinTemperatureF>67</MinTemperatureF>
                              <MaxTemperatureC>39</MaxTemperatureC>
                              <MinTemperatureC>19</MinTemperatureC>
                            </WeatherData>
                            <WeatherData>
                              <Day>Tuesday, May 12, 2009</Day>
                              <WeatherImage>http://forecast.weather.gov/images/wtf/skc.jpg</WeatherImage>
                              <MaxTemperatureF>101</MaxTemperatureF>
                              <MinTemperatureF>68</MinTemperatureF>
                              <MaxTemperatureC>38</MaxTemperatureC>
                              <MinTemperatureC>20</MinTemperatureC>
                            </WeatherData>
                            <WeatherData>
                              <Day>Wednesday, May 13, 2009</Day>
                              <WeatherImage>http://forecast.weather.gov/images/wtf/few.jpg</WeatherImage>
                              <MaxTemperatureF>103</MaxTemperatureF>
                              <MinTemperatureF>68</MinTemperatureF>
                              <MaxTemperatureC>39</MaxTemperatureC>
                              <MinTemperatureC>20</MinTemperatureC>
                            </WeatherData>
                            <WeatherData>
                              <Day>Thursday, May 14, 2009</Day>
                              <WeatherImage>http://forecast.weather.gov/images/wtf/few.jpg</WeatherImage>
                              <MaxTemperatureF>104</MaxTemperatureF>
                              <MinTemperatureF>68</MinTemperatureF>
                              <MaxTemperatureC>40</MaxTemperatureC>
                              <MinTemperatureC>20</MinTemperatureC>
                            </WeatherData>
                            <WeatherData>
                              <Day>Friday, May 15, 2009</Day>
                              <WeatherImage>http://forecast.weather.gov/images/wtf/few.jpg</WeatherImage>
                              <MaxTemperatureF>102</MaxTemperatureF>
                              <MinTemperatureF>69</MinTemperatureF>
                              <MaxTemperatureC>39</MaxTemperatureC>
                              <MinTemperatureC>21</MinTemperatureC>
                            </WeatherData>
                          </Details>
                        </GetWeatherByZipCodeResult>
                      </GetWeatherByZipCodeResponse>
                      
                       Hi Michael -
                      
                           I just realized that my data was in reverse.  
                           After the following line 
                              var myarr:Array = mystr.match(pattern); 
                               //add this line to reverse the array:
                              myarr = myarr.reverse();
                      
                           And if you want all 7 days...sorry thought it was 5 just increase the loop max.
                      
                                thanks,
                                   enjoy the weekend
                      • 8. Re: webservices - filtering xml to get at data
                        3rdshiftcoder Level 1

                        post with associated threads now closed....