dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: parsing/recursion issue...have to love this stuff

  1. #1
    ...in human form
    Join Date
    Aug 2002
    Posts
    188

    parsing/recursion issue...have to love this stuff

    okay you don't have to love it but i've got to figure it out. this is a problem for the big boys.

    i've got this parsing function which works pretty well except if there is an xml element nested inside of another element after a text node. so if the xml is as follows:

    Code:
    < unitname > flash kit < unitdate > October 7, 2002 < /unitdate > < /unitname >
    it parses it for display as:

    Code:
    < unitname > flash kit < /unitname >
    this is only for display as the xml is already loaded properly unchanged. but the whole point of the viewer is to display the xml properly. so can anyone think of a fix for the following code to handle this problem. my appreciation is overflowing in advance.

    this is the code. incidently what it is doing is creating an object for each node which is then displayed as a list of the objects in a viewer window.

    Code:
    function parseNodex (x, indent) {
        hName = x.nodeName;
        hValue = x.nodeValue;
        hType = x.nodeType;
        var xlevel = level;
        if (hType == 1) {
            // Element Type
            numNodes++;
            str = "<font color = \"#0000f7\">&lt;</font><font color = \"#996600\">"+hName+"</font>";
            a = x.attributes;
            for (i in a) {
                str += " <font color = \"#996600\">"+i+" </font><font color = \"#996600\"> = \"</font><b>"+a[i]+"</b><font color = \"#996600\">\"</font>";
            }
            str += "<font color = \"#0000f7\">&gt;</font>";
            level++;
            duplicateMovieClip ("/Viewer/main/el_0", "el_"+level, level);
            setProperty ("/Viewer/main/el_"+level, _y, getProperty("/Viewer/main/el_"+(level-1), _y)+20);
            setProperty ("/Viewer/main/el_"+level, _x, getProperty("/Viewer/main/el_"+level, _x)+indent);
            eval("/Viewer/main/el_"+level+":text") = str;
            if (x.hasChildNodes()) {
                if (x.firstChild.nodeType == 3) {
                    str = "<b>"+x.firstChild.nodeValue+"</b>";
                    str += "<font color = \"#0000f7\">&lt;/</font><font color = \"#996600\">"+x.nodeName+"</font><font color = \"#0000f7\">&gt;</font>";
                    eval("/Viewer/main/el_"+level+":text") += str;
                } else {
                    parseNodex(x.firstChild, indent+10);
                    level++;
                    str = "<font color = \"#0000f7\">&lt;/</font><font color = \"#996600\">"+x.nodeName+"</font><font color = \"#0000f7\">&gt;</font>";
                    duplicateMovieClip ("/Viewer/main/el_0", "el_"+level, level);
                    setProperty ("/Viewer/main/el_"+level, _y, getProperty("/Viewer/main/el_"+(level-1), _y)+20);
                    setProperty ("/Viewer/main/el_"+level, _x, getProperty("/Viewer/main/el_"+level, _x)+indent);
                    eval("/Viewer/main/el_"+level+":text") = str;
                    tellTarget ("/Viewer/main/el_"+(xlevel+1)) {
                        gotoAndStop (1);
                    }
                    eval("/Viewer/main/el_"+(xlevel+1)+":startnum") = xlevel+1;
                    eval("/Viewer/main/el_"+(xlevel+1)+":endnum") = level;
                    eval("/Viewer/main/el_"+(xlevel+1)+": p_m") = "-";
                }
            } else {
                str = "<font color = \"#0000f7\">&lt;/</font><font color = \"#996600\">"+x.nodeName+"</font><font color = \"#0000f7\">&gt;</font>";
                eval("/Viewer/main/el_"+level+":text") += str;
            }
        }
        h = x.nextSibling;
        while (h != null) {
            parseNodex(h, indent);
            h = h.nextSibling;
        }
    }
    Incarnations

  2. #2
    Jace Masula
    Join Date
    Apr 2000
    Posts
    100
    i dont see where the recursion occurs to allow the 'firstChild.firstChild.childnodes', i see where it grabs the values for firstChild.childnodes, but where exactly does it dive deeper?

  3. #3
    ...in human form
    Join Date
    Aug 2002
    Posts
    188
    I assumed that the nested parseNodex function at the if x.hasChildNodes would pick up the deeper levels and that is true except in the case where an element is the sibling of a textNode. I've tried adding a line which searched for siblings for textNodes. Here's the code:

    Code:
    function parseNodex (x, indent) {
    	hName = x.nodeName;
    	hValue = x.nodeValue;
    	hType = x.nodeType;
    	var xlevel = level;
    	if (hType == 1) {
    		// Element Type
    		numNodes++;
    		str = "<font color = \"#0000f7\">&lt;</font><font color = \"#996600\">"+hName+"</font>";
    		a = x.attributes;
    		for (i in a) {
    			str += " <font color = \"#996600\">"+i+" </font><font color = \"#996600\"> = \"</font><b>"+a[i]+"</b><font color = \"#996600\">\"</font>";
    		}
    		str += "<font color = \"#0000f7\">&gt;</font>";
    		level++;
    		duplicateMovieClip ("/Viewer/main/el_0", "el_"+level, level);
    		setProperty ("/Viewer/main/el_"+level, _y, getProperty("/Viewer/main/el_"+(level-1), _y)+20);
    		setProperty ("/Viewer/main/el_"+level, _x, getProperty("/Viewer/main/el_"+level, _x)+indent);
    		eval("/Viewer/main/el_"+level+":text") = str;
    		if (x.hasChildNodes()) {
    			if (x.firstChild.nodeType == 3) {
    				str = "<b>"+x.firstChild.nodeValue+"</b>";
    				eval("/Viewer/main/el_"+level+":text") += str;
    // this is where i try to fit it in...
    				if (x.firstChild != x.lastChild) {
    					parseNodex(x.firstChild.nextSibling, indent+10);
    				} else {
    					str += "<font color = \"#0000f7\">&lt;/</font><font color = \"#996600\">"+x.nodeName+"</font><font color = \"#0000f7\">&gt;</font>";
    					eval("/Viewer/main/el_"+level+":text") += str;
    				}
    			} else {
    				parseNodex(x.firstChild, indent+10);
    				level++;
    				str = "<font color = \"#0000f7\">&lt;/</font><font color = \"#996600\">"+x.nodeName+"</font><font color = \"#0000f7\">&gt;</font>";
    				duplicateMovieClip ("/Viewer/main/el_0", "el_"+level, level);
    				setProperty ("/Viewer/main/el_"+level, _y, getProperty("/Viewer/main/el_"+(level-1), _y)+20);
    				setProperty ("/Viewer/main/el_"+level, _x, getProperty("/Viewer/main/el_"+level, _x)+indent);
    				eval("/Viewer/main/el_"+level+":text") = str;
    				tellTarget ("/Viewer/main/el_"+(xlevel+1)) {
    					gotoAndStop (1);
    				}
    				eval("/Viewer/main/el_"+(xlevel+1)+":startnum") = xlevel+1;
    				eval("/Viewer/main/el_"+(xlevel+1)+":endnum") = level;
    				eval("/Viewer/main/el_"+(xlevel+1)+":p_m") = "-";
    			}
    		} else {
    			str = "<font color = \"#0000f7\">&lt;/</font><font color = \"#996600\">"+x.nodeName+"</font><font color = \"#0000f7\">&gt;</font>";
    			eval("/Viewer/main/el_"+level+":text") += str;
    		}
    	}
    	h = x.nextSibling;
    	while (h != null) {
    		parseNodex(h, indent);
    		h = h.nextSibling;
    	}
    }
    this however is causing it to write textNodes twice. Any ideas.
    Incarnations

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