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

    xml addChild - what's wrong with this?

    kimbflex Level 1

      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.


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical"
      <!-- local xml data provider -->
      <mx:HTTPService id="service" url="{getPath('a4m.xml')}"
      result="xmlData = service.lastResult as XML"/>
      <!-- logic -->
      import mdm.FileSystem;
      import mdm.Application;
      private var xmlData:XML;
      private function onCreationComplete():void{
      mdm.Application.init(this, onInitDone);
      private function onInitDone():void{
      private function updateXML():void{
      var newNode:XML = new XML('<story item="some item">a story</story>');
      var xml:XML = xmlData;
      // 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);
      // helper function used to provide correct paths at runtime in ZINC
      static private function getPath(aPath:String):String{
      var pathToUse:String = aPath;
      pathToUse = mdm.Application.path+aPath.split("/").join("\\");
      pathToUse = pathToUse.split("\\\\").join("\\");
      return pathToUse;
      <!-- 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()}"/>
      <!-- -->


      <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 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 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"/>