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

    problem processing xml in flex3


      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>
      <name>Plasma Television</name>
      <desc>65 inch screen with 1080p</desc>
      - <product>
      <name>Surround Sound Stereo</name>
      <desc>7.1 surround sound receiver with wireless speakers</desc>
      - <product>
      <desc>Bottom drawer freezer with water and ice on the door</desc>
      - <product>
      <desc>Large capacity with water saver setting</desc>
      - <product>
      <name>Leather Sectional</name>
      <desc>Plush leather with room for 6 people</desc>

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

      private function productListHandler(e:JavaFlexStoreEvent):void
                      productData = new ArrayCollection();
                      for each (var item:XML in JavaServiceHandler(e.currentTarget).response..product )
                          productData.addItem( {

      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.

        • 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");
                      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


            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");


                  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"?>




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



















                      private var





                      private function productListHandler(event:ResultEvent):void


                      productData =






                      for each (var item:XML in

                      event.result..product ){

                      productData.addItem( {














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




                      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]




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




                        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.