problem processing xml in flex3

Posted by john on Stack Overflow See other posts from Stack Overflow or by john
Published on 2009-06-11T23:17:24Z Indexed on 2010/04/21 12:03 UTC
Read the original article Hit count: 354

Filed under:

Hi All,

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.

The xml that is being returned from the server looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?><result 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></result>

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.

My JavaServiceHandler class looks like this:

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

	}

}

}

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.

Any help is appreciated.

Thanks

john

© Stack Overflow or respective owner

Related posts about flex