Groups | Blog | Home
all groups > flash actionscript > march 2004 >

flash actionscript : XML.nodeType doesn't work?


Frank sf
3/23/2004 10:10:20 PM
Hi,

I have a weird problem here, probably a bug in my code but I'm starting to
wonder...
nodeType never changes, it's always set to 1...
does anyone has a idea?

here's the code:

XML:
<main>
<search id="id1">
<criteria>...</criteria>
</search>
<search id="id2">
<criteria>...</criteria>
</search>
<criteria>criteriaX</criteria>
</main>

ActionScript, in the onload function...

var xml = this.firstChild.childNodes ;
var currentNode : XMLNode ;

for ( var a = 0 ; a < xml.length ; a++ )
{
currentNode = xml[a];
trace("item:" + a + " type:" + currentNode.nodeType );
trace( currentNode ); // this confirms me that I am on the right node

if (currentNode.nodeType == 1 )
{
trace("element: " + currentNode.attributes.id );
}

if ( currentNode.nodeType == 3 )
{
trace("text: " + currentNode.nodeValue );
}
}

the nodeType==3 never occur when it should at the last node
<criteria>criteriaX<criteria>, how come it says this last node is a Element
Node when it's Text Node??
I'm not an expert in XML so I might have done a huge mistake...

thanks in advance for your help

-Frank


Frank sf
3/23/2004 10:28:36 PM
JiZhang
3/24/2004 12:06:37 AM
add one more trace

trace("item:" + a + " type:" + currentNode.firstChild.nodeType ); // you will
see nodeType 3 :)

because trace("item:" + a + " type:" + currentNode.nodeType ); only trace to
<search id="id1">

A better way to check all xml nodes is to write a recursion method.
Frank sf
3/24/2004 12:36:07 AM
it works! thanks a lot...
I found it quite confusing though as I access the attributes without the need
of 'firstChild'...

I am indeed doing a recursive method, and I have another problem, it works
well for the first set of childs, but then it doesn't go further, it stalls at
the second level if I can say that...
I'm using currentNode.childNodes as the pointer to call the recursive function
again, you have another idea?

again, many thanks for your help :)
JiZhang
3/24/2004 12:52:29 AM
No worries.

You can download some flash xml source files from www.flashandxml.com

There are some examples of how you can make flash applications with xml and
database.

You can find recursive method of searching xml nodes in the source file.

Frank sf
3/24/2004 1:47:59 AM
AddThis Social Bookmark Button