Using variable "undefined" from XML

Hello all,

I have a situation where I am reading a wide variety of data from an XML file, and if a particular bit of data is not there, I simply want to hide a button.

Before I go further, all my XML data in and out works fine, that’s not a problem.

If I have this node in my XML:

<name1></name1>

read it in, assign to a variable and check that variable in the debugger I get this:

Variable _level0.assocImageName1 = undefined

This is fine. It has no value, so it’s undefined. Cool.

Now, if I do the most simple of If statements this check just doesn’t work. I’ve tried something like:

	if (_root.assocImageName1 == undefined) {
		_root.linked._visible = false;
	} else {
		break;
	}

Now the odd thing, is that with quotes (“undefined”) or without, this doesn’t work reliably. It seems to just run right past the If and perform the hiding of the button regardless.

Also, if I put something in the XML like so:

<name1>data</name1>

I get this in the debugger:

Variable _level0.assocImageName1 = "data"

and check for that like this it doesn’t work:

	if (_root.assocImageName1 == "data") {

but oddly enough, if I take the quotes off it does work. What’s going on there? I thought you also had to always use quotes when testing a string?

What’s going on here? This should be dead simple, shouldn’t it? What am I missing?

undefind isn’t really a preserved thing in flash, it just telling you the variable has no value, so using it in if statement should be like this::


	if (!_root.assocImageName1) {
		_root.linked._visible = false;
	} else {
		break;
	}

if _root.assocImageName1 is undefind (has no value, so its false) do something…

NEXT -

first of all, try not using _level0, but _root.
then: data (without quotes) is an preserved object in flash, telling flash it holds factual information, so it will return true, so you not actually testing a string, but testing if the variable holds factual data, witch is true, coz there is an string (get it? :))

it should work when using:


Variable _root.assocImageName1 = "data"
if (_root.assocImageName1 == "data") {

hope this makes allot clear

Hmm, thanks for the info. I should mention a few things. I wasn’t using _level0, I always use _root. I was just pointing out what the debugger in Flash was showing me. Also, I didn’t know about “data” being a reserved object. Interesting. However, I just threw that in as an example, I actually used other text content in there and it worked like I said in my original post (without quotes)??

I’ll give that !_root.assocImageName1 thing a try and see if that works for me.

Thanks! (I might be back soon, so don’t stray too far…)

Im not familiar with how the debugger handles objects/variables (I only assume it works much like trace) but my thinking is that you’re assigning assocImageName1 to be the text node and not the text. The output value of a text node is its nodeValue. Comparing that node does not compare against the nodeValue but rather the XMLNode object instance.

So, what you might try is:

if (_root.assocImageName1.nodeValue == “”) { … }
if (_root.assocImageName1.nodeValue == “data”) { … }

or

_root.assocImageName1 = (nodeReference).nodeValue;
//…
if (_root.assocImageName1 == “”) { … }
if (_root.assocImageName1 == “data”) { … }

I see what you are saying there. I should have mentioned that I am doing what you suggested in your second example. Previously in the code I am setting _root.assocImageName1 = nodeValue, and it is getting the value fine. The problem seems to lie in flash comparing that value for the If statement. It seems to get real confused.

In the debugger (i.e. if you command(alt)-return to test the movie and go to the debug menu and look at the variables) it shows that _root.assocImageName1 = “my content”, but if you test explicitly for “my content” it fails, and if you compare to just: my content (no quotes) it works. This seems to contradict the way flash ususally works.

And as I said before, if I try to check for nothing (= “”) or undefined that doesn’t work at all.

Well, I tried RvGate’s idea and that did not work. It did the same thing as testing for undefined or “”, it blows right past the If and hides the button regardless of whether there is data there.

I think I might try testing the nodeValue directly and see if that works…

mind you thats the text node nodeValue, not the elemtent’s

<name1>data</name1>

^ “data” is nameElementNode.firstChild.nodeValue;

and if there is no textNode you can check hasChildNodes() to see if it is empty or not

hmm, checking to see if hasChildNodes() is a good idea. I"ll have to try that, as the previous methods are going nowhere fast…