Note that there are some explanatory texts on larger screens.

plurals
  1. POProcessing XML in flex3
    text
    copied!<p>First time here asking a question and still learning on how to format things better... so sorry about the format as it does not look too well. I have started learning flex and picked up a book and tried to follow the examples in it. However, I got stuck with a problem. I have a jsp page which returns xml which basically have a list of products. I am trying to parse this xml, in other words go through products, and create Objects for each product node and store them in an ArrayCollection. The problem I believe I am having is I am not using the right way of navigating through xml.</p> <p>The xml that is being returned from the server looks like this:</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;&lt;result type="success"&gt; &lt;products&gt; &lt;product&gt; &lt;id&gt;6&lt;/id&gt; &lt;cat&gt;electronics&lt;/cat&gt; &lt;name&gt;Plasma Television&lt;/name&gt; &lt;desc&gt;65 inch screen with 1080p&lt;/desc&gt; &lt;price&gt;$3000.0&lt;/price&gt; &lt;/product&gt; &lt;product&gt; &lt;id&gt;7&lt;/id&gt; &lt;cat&gt;electronics&lt;/cat&gt; &lt;name&gt;Surround Sound Stereo&lt;/name&gt; &lt;desc&gt;7.1 surround sound receiver with wireless speakers&lt;/desc&gt; &lt;price&gt;$1000.0&lt;/price&gt; &lt;/product&gt; &lt;product&gt; &lt;id&gt;8&lt;/id&gt; &lt;cat&gt;appliances&lt;/cat&gt; &lt;name&gt;Refrigerator&lt;/name&gt; &lt;desc&gt;Bottom drawer freezer with water and ice on the door&lt;/desc&gt; &lt;price&gt;$1200.0&lt;/price&gt; &lt;/product&gt; &lt;product&gt; &lt;id&gt;9&lt;/id&gt; &lt;cat&gt;appliances&lt;/cat&gt; &lt;name&gt;Dishwasher&lt;/name&gt; &lt;desc&gt;Large capacity with water saver setting&lt;/desc&gt; &lt;price&gt;$500.0&lt;/price&gt; &lt;/product&gt; &lt;product&gt; &lt;id&gt;10&lt;/id&gt; &lt;cat&gt;furniture&lt;/cat&gt; &lt;name&gt;Leather Sectional&lt;/name&gt; &lt;desc&gt;Plush leather with room for 6 people&lt;/desc&gt; &lt;price&gt;$1500.0&lt;/price&gt; &lt;/product&gt; &lt;/products&gt;&lt;/result&gt; </code></pre> <p>And I have flex code that tries to iterate over products like following:</p> <pre><code>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 }); } } </code></pre> <p>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.</p> <p>My JavaServiceHandler class looks like this:</p> <pre><code>package com.wiley.jfib.store.data { import com.wiley.jfib.store.events.JavaFlexStoreEvent; import flash.events.Event; import flash.events.EventDispatcher; import flash.net.URLLoader; import flash.net.URLRequest; 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) ); } } } </code></pre> <p>Even though I refer to this as mine and it is not in reality. This is from a Flex book as a code sample which does not work, go figure.</p> <p>Any help is appreciated.</p> <p>Thanks</p> <p>john</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload