8 Replies Latest reply on Aug 17, 2006 1:07 PM by ntsiii

    Populate Tree with external XML

    BaniR
      HI every one.
      I'm trying to populate a tree controller from an external XML

      this is the code i use but something goes wrong
      <?xml version="1.0"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" height="400">

      <mx:Script>
      <![CDATA[
      import mx.collections.XMLListCollection;
      import mx.collections.ArrayCollection;


      // An XML object with categorized produce.
      var myXML:XML = new XML();
      var XML_URL:String = "assets/menu.xml";
      var myXMLURL:URLRequest = new URLRequest(XML_URL);
      var myLoader:URLLoader = new URLLoader(myXMLURL);
      myLoader.addEventListener("complete", xmlLoaded);

      function xmlLoaded(evtObj:Event) {
      myXML = XML(myLoader.data);
      trace("Data loaded.");
      }


      ]]>
      </mx:Script>

      <mx:Tree id="prodTree" dataProvider="{myXML}" width="200"
      showRoot="false" labelField="@label"/>

      </mx:Application>


      And thi is the XML file.......
      <?xml version="1.0"? format="e4x">
      <menuroot>
      <menu label="Menü 1bani" aktion="message" variables="Menü 1 Text...">
      <submenu label="Untermenü">
      <eintrag label="Eintrag"/>
      <eintrag label="Eintrag"/>
      <eintrag label="Eintrag"/>
      </submenu>
      <submenu label="Untermenü">
      <eintrag label="Eintrag"/>
      <eintrag label="Eintrag"/>
      <eintrag label="Eintrag"/>
      </submenu>
      </menu>
      <menu label="Menü 2">
      <eintrag label="Eintrag"/>
      <eintrag label="Eintrag"/>
      <eintrag label="Eintrag"/>
      </menu>
      <menu label="Menü 3">
      <submenu label="Untermenü">
      <eintrag label="Teil 1.1" aktion="message" variables="Teil 1.1"/>
      <eintrag label="Teil 1.2" aktion="message" variables="Teil 1.2"/>
      <eintrag label="Teil 1.3" aktion="message" variables="Teil 1.3"/>
      </submenu>
      <submenu label="Untermenü">
      <submenu label="Teil 2.1" aktion="message" variables="Teil 2.1">
      <eintrag label="Teil 2.1.1" aktion="message" variables="Teil 2.1.1"/>
      <eintrag label="Teil 2.1.2" aktion="message" variables="Teil 2.1.2"/>
      <eintrag label="Teil 2.1.3" aktion="message" variables="Teil 2.1.3"/>
      </submenu>
      <eintrag label="Teil 2.2" aktion="message" variables="Teil 2.2"/>
      <eintrag label="Teil 2.3" aktion="message" variables="Teil 2.3"/>
      </submenu>
      <eintrag label="Eintrag"/>
      </menu>
      <menu label="Menüs">
      <eintrag label="Hauptmenü" aktion="newMenu" variables="menu1.xml"/>
      <eintrag label="Links" aktion="newMenu" variables="menu2.xml"/>
      <eintrag label="Nachrichten" aktion="newMenu" variables="menu3.xml"/>
      </menu>
      </menuroot>


      i tried the xml internaly and it worked.
      where is the difference betwen an external XML and an internal one.
      can anybody please tell me what is wrong or what else can i do in this case.
      Thanks in advance
        • 1. Re: Populate Tree with external XML
          jrunrandy Adobe Employee
          Maybe try HTTPService. Flex SDK 2\samples\explorer\explorer.mxml uses this technique.

          HTH
          • 2. Re: Populate Tree with external XML
            ntsiii Level 3
            Isolate the problem.

            Trace, alert, or step through the code to make sure you are getting data.

            Tracy
            • 3. Re: Populate Tree with external XML
              ntsiii Level 3
              Yes, use HTTPService. I have seen a lot of folks having problems with the URLLoader thingy.
              • 4. Re: Populate Tree with external XML
                BaniR Level 1
                First of all thnx for your help.
                I tried the HTTP service but when i publish the Tree controller shows only a top node wich says [object HTTPService] and it doesn't interprets the XML. Where is the problem?

                I triedn also the <mx:XML source="assets/menu.xml"> and it worked fine
                but since I'm building an application who interacts with PHP i'm gona have to use the HTTPService.
                Another thing
                • 5. Re: Populate Tree with external XML
                  jrunrandy Adobe Employee
                  What are you using for resultFormat? The explorer sample (samples/explorer/explorer.mxml) uses resultFormat="e4x"
                  • 6. Re: Populate Tree with external XML
                    ntsiii Level 3
                    trace out the result object with toXMLString().

                    What is the dataprovider Binding expression you are using?

                    You might have to use a labelField or labelFunction.

                    Tracy
                    • 7. Re: Populate Tree with external XML
                      mmalpartida
                      Using (samples/explorer/explorer.mxml) as a guide here is a basic example which does the job but pay close attention to how the XML file is formatted as everytime I tried to use my own wacky xml it didnt work until I used the xml sample provided.

                      ----------------------freeTree.mxml----------------------
                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="treeReq.send()">
                      <mx:HTTPService
                      id="treeReq"
                      url="(enter your URL here)"
                      useProxy="false" resultFormat="e4x" result="treeLoaded();"/>


                      <mx:Script>
                      <![CDATA[


                      private function treeLoaded():void
                      {
                      treeList = XML(treeReq.lastResult.node);
                      }


                      ]]>
                      </mx:Script>

                      <mx:XML id="treeList"/>



                      <mx:Panel width="355" height="523" layout="absolute" x="10" y="10">
                      <mx:Tree id="myTree" height="465" width="315" labelField="@label" dataProvider="{treeList}" x="10" y="8" showRoot="false"></mx:Tree>
                      </mx:Panel>


                      </mx:Application>
                      ----------------------------------end freeTree.mxml---------------------------

                      Now here is a stripped down version of the xml explorer.mxml used

                      --------------------------------freeTree.xml-------------------------------

                      <?xml version="1.0" encoding="iso-8859-1"?>
                      <freeTree>
                      <node label="Super Node">

                      <node label="First Node">
                      <node label="First Child Node">
                      <node label="Hi!"/>
                      <node label="How are you?" />
                      <node label="Im not really a programmer" />
                      <node label="Just do it for a living" />
                      <node label="I like robots" />
                      <node label="Maybe I should build robots?" />
                      <node label="I like biochemistry as well"/>
                      <node label="Yeah I could be a scientist"/>
                      <node label="Too much school though"/>
                      <node label="Feel like playing Dead Rising right now" />
                      <node label="Killing zombies is fun"/>
                      <node label="Helps clear the mind"/>
                      <node label="And prepare one for the zombie-like life most of us have"/>
                      <node label="Sobe Adrenaline is the best tasting energy drink"/>
                      <node label="Good pick me up"/>
                      <node label="Just dont overdo those things"/>
                      <node label="They can get addictive"/>
                      <node label="Enjoy!"/>
                      </node>

                      </node>
                      </node>
                      </freeTree>



                      ------------------------------------end freeTree.mxml---------------------------
                      • 8. Re: Populate Tree with external XML
                        ntsiii Level 3
                        The tree can handle many (almost all?) xml formats.

                        The examples are created to use the defaults. To use your own you may need to specify labelField or labelFunction.

                        Tracy