[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Casting XML from HTTPService

Posted on 2011-10-23
3
Medium Priority
?
348 Views
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?


Thanks,

John


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


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

.....

initCatalog(remotetreedata);

Open in new window

0
Comment
Question by:thebellman
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
dgofman earned 2000 total points
ID: 37014337
When you defined a format e4x the results coming in XML if you would like to get top node(root) use a parent function.

cat.parent()
0
 

Author Closing Comment

by:thebellman
ID: 37014600
Thanks - Got it working.!
0
 

Author Comment

by:thebellman
ID: 37014631
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.

Sample.XML

<?xml version="1.0"?>
<top label="Hello">
                  <row label="Does this work?">
                        <row label="subnode 1"/>
                  </row>
                  <row label="node 3"> </row>
                  <row label="node 5"></row>
            </top>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
				 
				layout="absolute">
	
	<mx:Script>
		
		<![CDATA[
			import mx.events.IndexChangedEvent;
			import mx.events.ListEvent;
			import mx.events.TreeEvent;
			//import Tree.TreeRenderer;
			import mx.controls.Alert;
			import mx.controls.TextInput;
			import comps.*;
			import mx.rpc.AsyncToken;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.rpc.http.HTTPService;
			import mx.utils.ObjectUtil;
			import mx.collections.XMLListCollection;
			
			[Bindable] public var remotetreedata:XML;
			//public var set:TreeRenderer;
			[Bindable]
			public var set_visible:Boolean=false;
			public var value:String;
			
			private function add_label(value:String):void
			{
				
				// myTree.editedItemRenderer.data.@label=
				
				// 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) {
					event.preventDefault();
					
				}
			}
			
			// 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.
				event.preventDefault();
				
				// Get new phone number from editor.
				myTree.editedItemRenderer.data.@label =
					
					TreeEditor(event.currentTarget.itemEditorInstance)._foo.text;
				
				value=event.toString()
				Alert.show(value);
				
				// Get new status from editor.
				
				// Close the cell editor.
				// myTree.destroyItemEditor();
				
				// Notify the list control to update its display.
				//
				myTree.dataProvider.notifyItemUpdate(myTree.editedItemRenderer);
			}
			
			
			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);
			httpService.send();
			}
			
			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));
				
				
				initCatalog(remotetreedata);
			}
			
		]]>
	</mx:Script>
	
	

	
	
	<mx:Tree id="myTree"
			 width="400" height="400"
			 editable="true"
			  showRoot="false"
			 itemEditor="comps.TreeEditor"
			 editorHeightOffset="2" editorWidthOffset="-225"
			 editorXOffset="49" editorYOffset="25"
			 creationComplete="loadXMLData();"
			 itemEditBeginning="disableEditing(event);"
			 itemEditEnd="processData(event);" labelField="@label"/>
	
	<mx:Button x="524" y="120" label="Button" id="btn_add"
			   click="add_label(value)"/>
	
	
	
</mx:Application>

Open in new window

<?xml version="1.0" encoding="utf-8"?>
<!-- itemRenderers\sparkmx\myComponents\MyTreeItemRenderer.mxml -->
<s:MXTreeItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
					  xmlns:s="library://ns.adobe.com/flex/spark"
					  xmlns:mx="library://ns.adobe.com/flex/mx"
					  width="267" height="164" creationComplete="init();" >
	
	<fx:Script>
		<![CDATA[
			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;
			}
			
		]]>
	</fx:Script>
	
	<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(); "/>
			
		
	</s:Panel>
	
</s:MXTreeItemRenderer>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Suggested Courses

872 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