4 Replies Latest reply on Oct 4, 2007 1:42 PM by Puma1

    XML: Array problem

    Puma1
      Hello.
      I have the following Actionscript:


      var controlArray:Array;
      var variable:Array;
      var controlsXML:XML = new XML();
      controlsXML.ignoreWhite = true;
      controlsXML.onLoad = function(success:Boolean){
      if (success){
      var mainnode:XMLNode = controlsXML.firstChild;
      var controlNodes:Array = controlsXML.firstChild.childNodes;

      //trace (controlNodes);
      for (var i:Number = 0; i < controlNodes.length; i++) {
      var personnode:XMLNode = controlNodes .attributes.id;
      trace(personnode);
      var specificNode:Array = controlNodes
      .firstChild.childNodes;
      for (var j:Number = 0; j < specificNode.length; j++){
      var itemnode:Array = specificNode[j].nodeValue;
      trace(specificNode[j]);
      //trace(itemnode);
      }
      }

      } else {
      trace('error reading XML');
      }
      };
      controlsXML.load ("new.xml");


      And the following XML(new.xml):


      <?xml version="1.0" encoding="UTF-8"?>
      <phoneBook>
      <contact id="1">
      <name>Sas Jacobs</name>
      <name>John Furter</name>
      <name>Jo Ellen</name>
      </contact>
      <contact id="2">
      <name>John Smith</name>
      <name>Kell O'Brien</name>
      </contact>
      <contact id="3">
      <name>Jo Bloggs</name>
      <name>Mark Tull</name>
      </contact>
      </phoneBook>


      When I run the traces I get:

      1
      Sas Jacobs
      2
      John Smith
      3
      Jo Bloggs

      What I want is:

      1
      Sas Jacobs
      John Furter
      Jo Ellen
      2
      John Smith
      Kell O'Brien
      3
      Jo Bloggs
      Mark Tull

      The number of names can change and the actionscript would need to allow for this.
      What am I doing wrong?
      Thx!
        • 1. Re: XML: Array problem
          multi-task
          your going to have to do another loop on the childNodes of name.
          • 2. Re: XML: Array problem
            multi-task Level 1
            your going to have to do another loop on the childNodes of name.
            • 3. Re: XML: Array problem
              Puma1 Level 1
              Umm...I'm unclear on how to code that.
              I thought that the specificNode array with the "var j" loop would be the loop for the childNodes.
              Could you be more detailed? Thx
              • 4. Re: XML: Array problem
                Puma1 Level 1
                Figured it out. And now that I am looking at the code, I don't know why I wasn't getting it to begin with. Oh well.

                var controlArray:Array;
                var variable:Array;

                var controlsXML:XML = new XML();
                controlsXML.ignoreWhite = true;

                controlsXML.onLoad = function(success:Boolean){
                if (success){
                var mainnode:XMLNode = controlsXML.firstChild;
                var controlNodes:Array = controlsXML.firstChild.childNodes;
                for (var i:Number = 0; i < controlNodes.length; i++) {
                var personnode:XMLNode = controlNodes .attributes.id;
                trace(personnode);
                var specificNode:Array = controlNodes
                .childNodes;

                for (var j:Number = 0; j < specificNode.length; j++){
                var itemnode:XMLNode = specificNode[j].firstChild.nodeValue;
                trace(itemnode);

                }
                }

                } else {
                trace('error reading XML');
                }
                };
                controlsXML.load ("new.xml");