Question

Help,

In mxml I can load an xmlfile into an xmllistcollection at runtime using an httpservice, and then assign that to a list object.

I can also define an xml object menu tree structure in the code and assign it to a tree.

What I can't do is load the xml file at runtime and assign it to a tree.

So I'm trying to load a file:

<root>
 <menuitem name="First Main Item">
   <menuitem name = "sub item 1"/>
   <menuitem name = "sub item 2"/>
 </menuitem>
 <menuitem name="First Main Item">
   <menuitem name = "sub item 3"/>
   <menuitem name = "sub item 4"/>
 </menuitem>
</root>

into a tree at runtime.

Any clues as to how to do this?

Was it helpful?

Solution

Just use a HTTPService to send a request to your URL. The Tree component uses a List as data provider, so you need to use

dataProvider="{myXml.menuitem}"

to convert your XML to a List. Don't forget to define a labelField of your tree

labelField="@name"

Here is the Tree:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="955" minHeight="600" 
           creationComplete="myService.send()">
<fx:Declarations>
    <s:HTTPService id="myService" url="com/xmltree/tree.xml" resultFormat="e4x" result="onServiceResult(event)"/>
</fx:Declarations>

<fx:Script>
    <![CDATA[
        import mx.rpc.events.ResultEvent;

        [Bindable]private var myXml:XML;

        private function onServiceResult(event:ResultEvent):void
        {
            myXml = event.result as XML;
        }
    ]]>
</fx:Script>

<s:HGroup x="20" y="20">
    <mx:Tree 
        dataProvider="{myXml.menuitem}" 
        labelField="@name"/>
</s:HGroup>

</s:Application>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top