8 Replies Latest reply on Sep 25, 2009 11:07 PM by SunWuKonga

    problem processing xml in flex3

    aja321

      Hi All,

       

      I am just starting to learn flex and got stuck on processing/parsing xml data in flex client.

      I have a jsp page which returns xml. Returned xml contains this:

       

       

      <?xml  version="1.0" encoding="ISO-8859-1" ?>
      - <results type="success">
      - <products>
      - <product>
      <id>6</id>
      <cat>electronics</cat>
      <name>Plasma Television</name>
      <desc>65 inch screen with 1080p</desc>
      <price>$3000.0</price>
      </product>
      - <product>
      <id>7</id>
      <cat>electronics</cat>
      <name>Surround Sound Stereo</name>
      <desc>7.1 surround sound receiver with wireless speakers</desc>
      <price>$1000.0</price>
      </product>
      - <product>
      <id>8</id>
      <cat>appliances</cat>
      <name>Refrigerator</name>
      <desc>Bottom drawer freezer with water and ice on the door</desc>
      <price>$1200.0</price>
      </product>
      - <product>
      <id>9</id>
      <cat>appliances</cat>
      <name>Dishwasher</name>
      <desc>Large capacity with water saver setting</desc>
      <price>$500.0</price>
      </product>
      - <product>
      <id>10</id>
      <cat>furniture</cat>
      <name>Leather Sectional</name>
      <desc>Plush leather with room for 6 people</desc>
      <price>$1500.0</price>
      </product>
      </products>
      </results>

      And I have flex code that tries to iterate over products like following:

      private function productListHandler(e:JavaFlexStoreEvent):void
                  {
                      productData = new ArrayCollection();
                      trace(JavaServiceHandler(e.currentTarget).response);
                      for each (var item:XML in JavaServiceHandler(e.currentTarget).response..product )
                      {
                          productData.addItem( {
                              id:item.id,
                              item:item.name,
                              price:item.price,
                              description:item.desc
                          });
                      }
                  }

      with trace, I can see the xml being returned from the server. However, I cannot get inside the loop as if the xml was empty. In other words, JavaServiceHandler(e.currentTarget).response..product must be returning nothing. Can someone please help/point out what I could be doing wrong.

      Thanks
      aja
        • 1. Re: problem processing xml in flex3
          aja321 Level 1

          My JavaServiceHandler class looks like this:

           

          public class JavaServiceHandler extends EventDispatcher
              {
                  public var serviceURL:String = "";
                  public var response:XML;
                 
                  public function JavaServiceHandler()
                  {
                  }
                 
                  public function callServer():void
                  {
                      if(serviceURL == "")
                      {
                          throw new Error("serviceURL is a required parameter");
                          return;
                      }
                     
                      var loader:URLLoader = new URLLoader();
                      loader.addEventListener(Event.COMPLETE, handleResponse);
                      loader.load(new URLRequest(serviceURL));
                     
          //            var httpService:HTTPService  = new HTTPService();
          //            httpService.url = serviceURL;
          //            httpService.resultFormat = "e4x";
          //            httpService.addEventListener(Event.COMPLETE, handleResponse);
          //            httpService.send();
                     
                  }
                 
                  private function handleResponse(e:Event):void
                  {
                      var loader:URLLoader = URLLoader(e.currentTarget);
                      response = XML(loader.data);
                      dispatchEvent(new JavaFlexStoreEvent(JavaFlexStoreEvent.DATA_LOADED) );

           

          //            var httpService:HTTPService = HTTPService(e.currentTarget);
          //            response = httpService.lastResult.product;
          //            dispatchEvent(new JavaFlexStoreEvent(JavaFlexStoreEvent.DATA_LOADED) );
                     
                  }

           

              }

          • 2. Re: problem processing xml in flex3
            Celinio

            hi,

            could you fix it? I am facing the same problem.

            I saw you also posted question at http://stackoverflow.com/questions/984284/problem-processing-xml-in-flex3

             

            Still no solution ? Let me know please, thanks.

            • 3. Re: problem processing xml in flex3
              *Prashant Shelke* Level 4

              JavaServiceHandler(e.currentTarget).response.products.product bold word is missing I thinks so there.

              • 4. Re: problem processing xml in flex3
                leybniz Level 4

                private function handleResponse(e:Event):void
                        {
                            var loader:URLLoader = URLLoader(e.currentTarget);
                            response = XML(loader.data);

                            response = new XML(loader.data);

                 

                If you feel this message answers your question or helps, please mark it respectively

                • 5. Re: problem processing xml in flex3
                  Celinio Level 1

                  Replacing response = XML(loader.data); by            response = new XML(loader.data); does not change anything.

                   

                  I have runned the debugger and i have noticed in the debug window that the XML tags are not properly displayed.

                  > is rendered as &gt;

                  < is rendered as &lt;

                   

                  I have made a change in the  callServer() function, by using an XML file with the content i am supposed to get :

                   

                  var urlRequest:URLRequest = new URLRequest("xmltest.xml");
                  loader.load(urlRequest);

                   

                  instead of

                   

                  loader.load(new URLRequest(serviceURL));

                   

                  And that works. So maybe the problem comes from the backend, since the author uses Java classes with the same toXml() method :

                   

                  public String toXml() {
                  String xml = "<product>";
                  xml += "<id>" + id + "</id>";
                  xml += "<cat>" + category + "</cat>";
                  xml += "<name>" + name + "</name>";
                  xml += "<desc>" + description + "</desc>";
                  xml += "<price>$" + price + "</price>";
                  xml += "</product>";
                  return xml;
                  }

                   

                  Is there any way i can handle the XML tags ? Either in ActionScript or Java.

                   

                  Thanks in advance for helping.

                  • 6. Re: problem processing xml in flex3
                    Celinio Level 1

                    Thanks but that's not working either.

                    • 7. Re: problem processing xml in flex3
                      rtalton Level 4

                      I put together a simple example of looping through xml results for you:

                       

                       

                       

                       

                       

                       

                       

                       

                       

                      <?xml version="1.0" encoding="utf-8"?>

                      <mx:Application

                       

                       

                      xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute

                      "

                      creationComplete="myData.send()"

                       

                      >

                       

                      <mx:Script>

                      <![CDATA[

                       

                       

                      import

                      mx.collections.ArrayCollection;

                       

                       

                      import

                      mx.rpc.events.ResultEvent;

                       

                       

                       

                      private var

                      productData:ArrayCollection;

                       

                       

                       

                      private function productListHandler(event:ResultEvent):void

                      {

                      productData =

                       

                      new

                      ArrayCollection();

                       

                       

                      for each (var item:XML in

                      event.result..product ){

                      productData.addItem( {

                      id:item.id.toString(),

                      item:item.name.toString(),

                      price:item.price.toString(),

                      description:item.desc.toString()

                      });

                       

                      }

                      }

                      ]]>

                      </mx:Script>

                      <mx:HTTPService

                       

                       

                      id="myData" url="data/data.xml" result="productListHandler(event)" resultFormat="e4x" />

                       

                      </mx:Application>

                       

                      But you must know that you can simply return the results as "object" (the default), and then you don't have to loop through the results at all.

                      • 8. Re: problem processing xml in flex3 [Solution]
                        SunWuKonga

                        Replace

                         

                         

                        for each (var item:XML in JavaServiceHandler(e.currentTarget).response..product)
                        

                         

                        with

                         

                        for each (var item:XML in XML(e.currentTarget.response.toString())..product)
                        

                         

                        That'll will fix it. However I don't know why the original doesn't work.