2 Replies Latest reply on Oct 25, 2006 2:59 AM by kimbflex

    xml addChild - what's wrong with this?

    kimbflex
      Hello,

      I'm trying to insert a node into an xml file which is a dataProvider for a Tree.

      It works if I take out the sub-nodes of the xml file, it works if I take out the Tree.

      It fails with:
      TypeError: Error #1086: The appendChild method only works on lists containing one item.
      if I use both.

      An obvious reason however how do I make it work for both? Code and xml is below.

      Kim


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical"
      horizontalAlign="left"
      creationComplete="onCreationComplete();">
      <!-- local xml data provider -->
      <mx:HTTPService id="service" url="{getPath('a4m.xml')}"
      resultFormat="e4x"
      result="xmlData = service.lastResult as XML"/>
      <!-- logic -->
      <mx:Script>
      <![CDATA[
      import mdm.FileSystem;
      import mdm.Application;
      [Bindable]
      private var xmlData:XML;
      private function onCreationComplete():void{
      service.send();
      try{
      mdm.Application.init(this, onInitDone);
      }catch(e:Error){
      }
      }
      private function onInitDone():void{
      executeBindings(false);
      }
      private function updateXML():void{
      var newNode:XML = new XML('<story item="some item">a story</story>');
      var xml:XML = xmlData;
      xml..users.appendChild(newNode);
      out.executeBindings(false);
      try{
      // update local file
      var whereToSave:String = mdm.Application.path+"a4m.xml";
      whereToSave = whereToSave.split("\\\\").join("\\");
      var whatToSave:String = xml.toXMLString();
      mdm.FileSystem.saveFileUnicode(whereToSave, whatToSave);
      }catch(e:Error){}
      }
      // helper function used to provide correct paths at runtime in ZINC
      static private function getPath(aPath:String):String{
      var pathToUse:String = aPath;
      try{
      pathToUse = mdm.Application.path+aPath.split("/").join("\\");
      pathToUse = pathToUse.split("\\\\").join("\\");
      }catch(e:Error){}
      return pathToUse;
      }
      ]]>
      </mx:Script>
      <!-- xml collection -->
      <mx:XMLListCollection id="stories" source="{xmlData..story}"/>
      <mx:ComboBox id="combo" dataProvider="{stories}" labelField="@item"/>
      <mx:Button label="Save/Update XML" click="callLater(updateXML);"/>
      <!-- -->
      <!-- view -->
      <mx:Tree height="317" dataProvider="{stories}" showRoot="false" labelField="@item" width="269"></mx:Tree>
      <mx:TextArea id="out" width="270" height="183" text="{xmlData.toXMLString()}"/>
      <!-- -->
      </mx:Application>

      a4m.mxml:

      <root>
      <stories>
      <story item="My Story 1">
      <test item="MyTest 1" testtype="1">
      <i item="video" file="video/science01.flv" view="1"/>
      <i item="video" file="video/science02.flv" view="2"/>
      <i item="button" text="Talk to Him" audio="audio/talktohim.mp3" link="MyTest 3"/>
      <i item="button" text="Ignore Him" audio="audio/ignorehim.mp3" link="MyTest 2"/>
      </test>
      <test item="MyTest 2" testtype="1">
      <i item="video" file="video/science03.flv" view="1"/>
      <i item="video" file="video/science06.flv" view="1"/>
      <i item="video" file="video/science04.flv" view="2"/>
      <i item="button" text="Talk to Him" audio="audio/talktohim.mp3" link="MyTest 3"/>
      <i item="button" text="Do homework" audio="audio/dohomework.mp3" link="MyTest 1"/>
      </test>
      <test item="MyTest 3" testtype="1">
      <i item="video" file="video/science05.flv" view="1"/>
      <i item="video" file="video/science07.flv" view="2"/>
      <i item="button" text="Help" audio="audio/help.mp3" link="MyTest 4"/>
      <i item="button" text="Exit" audio="audio/exit.mp3" link="Exit"/>
      </test>
      </story>
      </stories>
      </root>