dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: strange xml recursion problem

  1. #1
    Member
    Join Date
    Jul 2001
    Location
    NYC, NY
    Posts
    62

    strange xml recursion problem

    I'm new to the xml/flash thing, though not new to programming in general. Anyway, I have a fairly simple xml file I'm working with, that looks like this:

    Code:
    <mapData>
    <name>Louisiana</name>
    <abbr>La</abbr>
    <map>la.png</map>
      <community>
        <id>1</id>
        <title>Spring Woods</title>
        <city>New Orleans</city>
        <phone>555.5555</phone>
        <coords>
          <xCoord>11</xCoord>
          <yCoord>11</yCoord>
        </coords>
      </community>
      <community>
        <id>2</id>
        <title>Swamp Heights</title>
        <city>Baton Rouge</city>
        <phone>666.6666</phone>
        <coords>
          <xCoord>12</xCoord>
          <yCoord>12</yCoord>
        </coords>
      </community>
    </mapData>
    I'm simply trying to run through the xml recursively, but my recursive function magically craps out after going into the <name> node. I can't figure out for the life of me why it won't continue iterating through each sibling node and just goes in as deep as it can then craps out. Here's the function code:

    Code:
    function traceNodeNames(curr_node) {
    	if(curr_node.hasChildNodes()) {
    		trace("current Node name = " + curr_node.nodeName);
    		children = curr_node.childNodes;
    		trace ("Number of Children = " + children.length);
    		for(i = 0; i < children.length; i++) {
    			trace("total children = " + children.length + ", current child = " + i);
    			x = traceNodeNames(children[i]);
    			trace(x);
    		}
    		return i;
    	}
    	else {
    		trace("value = " + curr_node.nodeValue);
    		return 0;
    	}
    }
    Anyone know why this isn't working? I've tried using the debugging tool as well, and when I step through it still just magically pops out of the recursion after getting into the <name> node. Any help with this would be great. Or if I'm totally wrong in assuming this sort of recursion would work in flash, what is a better way of pulling out data from an XML file so that I can use it to populate variables and mc's in a swf? Oh, I'm using MX2k4 btw.
    -Suki

  2. #2
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    Hmm, your code works fine for me, maybe you initally pass the wrong
    node into the recursive function?
    Here the full code i tested your xml with:
    Code:
    var xml:XML = new XML();
    xml.ignoreWhite = true;
    xml.onLoad = parse;
    xml.load("bla.xml");
    function parse () {
    	traceNodeNames(this);
    }
    
    function traceNodeNames(curr_node) {
    	if(curr_node.hasChildNodes()) {
    		trace("current Node name = " + curr_node.nodeName);
    		children = curr_node.childNodes;
    		trace ("Number of Children = " + children.length);
    		for(i = 0; i < children.length; i++) {
    			trace("total children = " + children.length + ", current child = " + i);
    			x = traceNodeNames(children[i]);
    			trace(x);
    		}
    		return i;
    	}
    	else {
    		trace("value = " + curr_node.nodeValue);
    		return 0;
    	}
    }
    My letters on the F1 key have faded, how are yours today?

  3. #3
    Member
    Join Date
    Jul 2001
    Location
    NYC, NY
    Posts
    62
    Thanks for the reply McUsher. I wonder if my file is corrupt or something. I copied your code verbatim and this is the output I got:

    current Node name = null
    Number of Children = 1
    total children = 1, current child = 0
    current Node name = mapData
    Number of Children = 5
    total children = 5, current child = 0
    current Node name = name
    Number of Children = 1
    total children = 1, current child = 0
    value = Louisiana
    0
    1
    2

    As you can see, it doesn't get any further than that <name> node, then for some reason just jumps out of the recursive function. This isn't the output you were getting is it? I'm trying to get it to trace all the text nodes basically, more of a proof-of-concept than anything else.
    -Suki

  4. #4
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    Oh.. dude.. i am sorry.. i got the same results... didn't look very close on
    it and thought everything is alright.

    I looked again and found the mistake:
    You need to make every single variable in your function exist
    only in local context, so the recursion doesn't know of variables
    outside:
    Code:
    function traceNodeNames(curr_node) {
    	if(curr_node.hasChildNodes()) {
    		trace("current Node name = " + curr_node.nodeName);
    		var children = curr_node.childNodes;
    		trace ("Number of Children = " + children.length);
    		for(var i = 0; i < children.length; i++) {
    			trace("total children = " + children.length + ", current child = " + i);
    			var x = traceNodeNames(children[i]);
    			trace(x);
    		}
    		return i;
    	}
    	else {
    		trace("value = " + curr_node.nodeValue);
    		return 0;
    	}
    }
    the tiny word "var" infront of each variable makes it local, so
    it exists only within one instance of that function
    My letters on the F1 key have faded, how are yours today?

  5. #5
    Member
    Join Date
    Jul 2001
    Location
    NYC, NY
    Posts
    62
    Thanks McUsher, that seems to have cleared it all up. Of everything involved in programming, scope seems to be the one that bites me most often, and it doesn't help that most languages seem to handle it slightly differently. So I guess the children and i variables were being re-written with each recursion, so at the bottom of a branch, since the parent variables were re-written they no longer existed. That explains why it was exiting prematurely.

    Thanks again!
    -Suki

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center