0 Replies Latest reply on Sep 21, 2006 6:13 AM by ParomitaDey15

    Writing XML data

    ParomitaDey15
      Hi,
      This may be a little silly but I am completely stuck and unable to move ahead.
      I am trying to traverse an Arraycollection data and create a XML stream from the arraycollection data. The repeated items from the arraycollection will be present only once in the xml.

      Arraycollection
      user: User1
      project: project1
      date: 09/19/2006

      user: User2
      project: porject1
      date:09/20/2006

      user:User1
      project: project2
      date:09/20/2006

      user:User1
      project: project2
      date:09/21/2006

      I need the output as
      <root>
      <node label='User1'>
      <node label='project1'>
      <node label='09/19/2006'/>
      </node>
      <node label='project2'>
      <node label='09/20/2006'/>
      <node label='09/21/2006'/>
      </node>
      </node>
      <node label='User2'>
      <node label='project1'>
      <node label='09/20/2006'/>
      </node>
      </node>
      </root>

      To create the mentioned xml, I was using a for each loop to traverse to the leaf elements. But when I try to add a sibling to any existing leaf using insertChildBefore or after, it is giving a null reference error. If I try to trace the leaf node using xml.child('childname') ... it is giving a null reference error. I am pasting the code below -

      private function GetXMLTree():void{
      var tmpXML:XML = new XML("<root></root>");
      var tmpLicen:String = tmpXML.licen.(@dtype == 'licensee').@label;
      var tmpProj:String = tmpXML.proj.(@dtype == 'project').@label;
      for (var i:int = 0; i < tiledata.length; i++) {
      if(tmpLicen.indexOf(tiledata.getItemAt(i).licensee.toString()) == -1){
      var newnode:XML = new XML();
      newnode =
      <licen label={tiledata.getItemAt(i).licensee.toString()} dtype='licensee'>
      <proj label={tiledata.getItemAt(i).project.toString()} dtype='project'>
      </proj>
      </licen>;
      tmpXML = tmpXML.appendChild(newnode);
      }else{
      for each (var tstLicen:XML in tmpXML.licen){
      var x:String = tstLicen.@dtype;
      var y:String = tstLicen.@label;

      if((x.indexOf('licensee') != -1) &&
      (y.indexOf(tiledata.getItemAt(i).licensee.toString()) != -1)){
      var childnew:XML = new XML("<proj label='" + tiledata.getItemAt(i).project.toString() + "' dtype='project'></proj>");
      // Alert.show(tstLicen.child("proj").toXMLString());
      tmpXML = tmpXML.insertChildAfter(tstLicen.child("proj").,childnew.toXMLString());
      }

      }

      }
      tmpLicen = tmpXML.licen.(@dtype == 'licensee').@label;
      tmpProj = tmpXML.proj.(@dtype == 'project').@label;
      }
      txtTest.text = tmpXML.toString();
      }
      // txtTest - this is a textarea control where I was trying to see the generated xml. and tiledata is an arraycollection containing data as -
      <asset>
      <assetid>1</assetid>
      <filename>images/library/Bluehills.jpg</filename>
      <assetname>Bluehills.jpg</assetname>
      <filetype>jpg</filetype>
      <datetime>09/16/2006</datetime>
      <licensee>Christys</licensee>
      <project>Hotwheels Caps</project>
      </asset>
      <asset>
      <assetid>1</assetid>
      <filename>images/library/Bluehills.jpg</filename>
      <assetname>Bluehills.jpg</assetname>
      <filetype>jpg</filetype>
      <datetime>09/15/2006</datetime>
      <licensee>Christys</licensee>
      <project>Hotwheels T</project>
      </asset>
      <asset>
      <assetid>1</assetid>
      <filename>images/library/Bluehills.jpg</filename>
      <assetname>Bluehills.jpg</assetname>
      <filetype>jpg</filetype>
      <datetime>09/16/2006</datetime>
      <licensee>Marvel</licensee>
      <project>Hotwheels TA</project>
      </asset>

      Please advice.

      Thanks and Regards,
      Paromita