5 Replies Latest reply on Feb 28, 2008 3:03 AM by Newsgroup_User

    Objects returned from Classes

    ChrisFlynn Level 1
      Hi all

      I have a question about classes returning objects.

      I have something that looks like this on my timeline:

      var myXML:XML = new XML(); //<-- loaded externally
      var myObject:ObjectFromXML = new ObjectFromXML();

      myObject.buildObjFromXML(myXML);

      I also have a class called "ObjectFromXML" which looks like this:

      class ObjectFromXML {
      function ObjectFromXML() {
      }
      function buildObjFromXML(thisXMLNode:XMLNode):Object {

      var obj:Object = new Object();

      for (var attr in thisXMLNode.attributes) {
      var thisAttr:String = attr;
      obj[thisAttr] = new Object();
      obj[thisAttr] = thisXMLNode.attributes[attr];
      }
      if (thisXMLNode.hasChildNodes) {
      var XMLNodeLength:Number = thisXMLNode.childNodes.length;
      var cnt:Number = 0;
      var suffix:String = "";

      for (var i = 0; i<=(XMLNodeLength-1); i++) {

      var nodeName:String = thisXMLNode.childNodes .nodeName;
      var nextNode:String = thisXMLNode.childNodes[(i+1)].nodeName;
      if (nodeName == nextNode) {
      cnt++;
      suffix = cnt.toString();
      } else {
      if (cnt != 0) {
      cnt++;
      suffix = cnt.toString();
      } else {
      suffix = "";
      }
      cnt = 0;
      }
      obj[nodeName+suffix] = new Object();
      if ((thisXMLNode.childNodes
      .firstChild.nodeValue != undefined) || (thisXMLNode.childNodes .firstChild.nodeValue != null)) {
      obj[nodeName+suffix] = thisXMLNode.childNodes
      .firstChild.nodeValue;
      } else {
      obj[nodeName+suffix] = buildObjFromXML(thisXMLNode.childNodes );

      }
      }
      }
      trace("\n\n");
      for (var j in obj) {
      trace(j+" = "+obj[j]);
      for (var k in obj[j]) {
      trace(k+" = "+obj[j][k]);
      }
      }
      return obj;
      }
      }

      It seems like all the code is in the Class is working the way I want it to. I know this because the loops that trace the contents of obj at the end are giving me what I expect.

      But from the timeline, if I trace the myObject in the same way, I get nothing.

      I'm a bit of a newb when it comes to writing classes, so if anyone can see what I'm doing wrong, let me know.

      Thanks
      Chris