Casting XML from HTTPService

Posted on 2011-10-23
Last Modified: 2012-05-12
I'm hoping someone can help - I've spent way too much time trying to get something to work.

I'm trying to setup a tree structure that the user can edit.  I want to use an itemrender and a few events to handle the edits.  The only way I've gotten this to work nicely is to feed the tree an object that is created by Flex at compile time.  What I need is the ability to read in the data at runtime.

If I use a MX:XML model, Flex casts the XML as an object and everything works perfectly.

What I can't figure out is how to read this same data from an HTTP Service call.  I've tried setting the result type to object, e4x, xml etc.  I've tried XMLList, XML, and Array Collections.

I need the XML  cast as an object in the same manner that Flex stores/compiles it using the <mx:XML> model.

Any ideas of thoughts?



<mx:XML id="treedata"  format="e4x">
		<top label="node 0">
			<row label="node 1">
				<row label="subnode 1"/>
			<row label="node 3"> </row>
			<row label="node 5"></row>

private function initCatalog(cat:Object):void {
				myTree.dataProvider = cat;



Open in new window

Question by:thebellman
    LVL 29

    Accepted Solution

    When you defined a format e4x the results coming in XML if you would like to get top node(root) use a parent function.


    Author Closing Comment

    Thanks - Got it working.!

    Author Comment

    In case someone is looking for a similar solution - the code example is shown below.  This example provides a tree control, read from external XML and allows you to edit.


    <?xml version="1.0"?>
    <top label="Hello">
                      <row label="Does this work?">
                            <row label="subnode 1"/>
                      <row label="node 3"> </row>
                      <row label="node 5"></row>
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx=""
    			//import Tree.TreeRenderer;
    			import mx.controls.Alert;
    			import mx.controls.TextInput;
    			import comps.*;
    			import mx.rpc.AsyncToken;
    			import mx.rpc.http.HTTPService;
    			import mx.utils.ObjectUtil;
    			import mx.collections.XMLListCollection;
    			[Bindable] public var remotetreedata:XML;
    			//public var set:TreeRenderer;
    			public var set_visible:Boolean=false;
    			public var value:String;
    			private function add_label(value:String):void
    				// nav.editedItemPosition={rowIndex:event.rowIndex}
    			private function initCatalog(cat:Object):void {
    				myTree.dataProvider = cat;
    				trace ("My provider:" + myTree.dataProvider);
    			// Define the event listener for the itemEditBeginning event
    			// to disable editing when the user selects
    			// the top node in the tree.
    			private function disableEditing(event:ListEvent):void {
    				if(event.rowIndex==0) {
    			// Define the event listener for the itemEditEnd event
    			// to copy the updated data back to the data provider
    			// of the Tree control.
    			public function processData(event:ListEvent):void {
    				// Disable copying data back to the control.
    				// Get new phone number from editor. =
    				// Get new status from editor.
    				// Close the cell editor.
    				// myTree.destroyItemEditor();
    				// Notify the list control to update its display.
    			private function loadXMLData():void {
    				var httpService:HTTPService = new HTTPService();
    			httpService.url = "xml/sample.xml" ;
    			httpService.resultFormat = "e4x";
    			httpService.addEventListener(FaultEvent.FAULT, httpService_fault);
    			httpService.addEventListener(ResultEvent.RESULT, httpService_result);
    			private function httpService_fault(evt:FaultEvent):void {
    				var title:String = evt.type + " (" + evt.fault.faultCode + ")";
    				var text:String = evt.fault.faultString;
    			private function httpService_result(evt:ResultEvent):void {
    				trace ('Made it here');
    				remotetreedata  = XML(evt.result) ;
    				trace('The object:' + ObjectUtil.toString(remotetreedata));
    	<mx:Tree id="myTree"
    			 width="400" height="400"
    			 editorHeightOffset="2" editorWidthOffset="-225"
    			 editorXOffset="49" editorYOffset="25"
    			 itemEditEnd="processData(event);" labelField="@label"/>
    	<mx:Button x="524" y="120" label="Button" id="btn_add"

    Open in new window

    <?xml version="1.0" encoding="utf-8"?>
    <!-- itemRenderers\sparkmx\myComponents\MyTreeItemRenderer.mxml -->
    <s:MXTreeItemRenderer xmlns:fx=""
    					  width="267" height="164" creationComplete="init();" >
    			import mx.collections.*;
    			import mx.controls.Tree;
    			import mx.controls.treeClasses.*;
    			public var newCategory:String;
    			private function init ():void {
    				labelDisplay.text = treeListData.label;
    			// Override the set method for the data property
    			// to set the font color and style of each node.    
    			private function close ():void {
    				var tree:Tree = listData.owner as Tree;
    				tree.editedItemPosition = null;
    				tree.selectedIndex = -1;
    	<s:Panel x="10" y="10" width="250" height="131">
    			<s:Label id="labelDisplay" x="25" y="10"/>
    			<s:TextInput id="_foo" x="74" y="9" width="150" text='hello' />
    			<s:Button x="1" y="67" label="Save it!" click="close();"/>
    		<s:Button x="178" y="67" label="Cancel" click="parentDocument.mytree.destroyItemEditor(); "/>

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
    There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
    The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
    This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now