# Using variable "undefined" from XML

**URL:** <https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078>\
**Category:** Uncategorized\
**Created:** [August 24, 2004, 8:48pm UTC](https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078 "2004-08-24T20:48:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xMetal](https://avatars.discourse-cdn.com/v4/letter/x/c6cbf5/32.png) [@xMetal](https://forum.kirupa.com/u/xMetal)\
**Post date:** [August 24, 2004, 8:48pm UTC](https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078/1 "2004-08-24T20:48:26Z")

</div>

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:

```auto
<name1></name1>

```

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

```auto
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:

```auto
	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:

```auto
<name1>data</name1>

```

I get this in the debugger:

```auto
Variable _level0.assocImageName1 = "data"

```

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

```auto
	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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 25, 2004, 2:53pm UTC](https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078/2 "2004-08-25T14:53:49Z")

</div>

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::

```php

	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:

```php

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

```

hope this makes allot clear

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 25, 2004, 3:05pm UTC](https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078/3 "2004-08-25T15:05:03Z")

</div>

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…)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 25, 2004, 3:06pm UTC](https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078/4 "2004-08-25T15:06:54Z")

</div>

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”) { … }

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 25, 2004, 3:15pm UTC](https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078/5 "2004-08-25T15:15:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 25, 2004, 3:24pm UTC](https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078/6 "2004-08-25T15:24:02Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 25, 2004, 3:26pm UTC](https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078/7 "2004-08-25T15:26:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 25, 2004, 3:34pm UTC](https://forum.kirupa.com/t/using-variable-undefined-from-xml/62078/8 "2004-08-25T15:34:24Z")

</div>

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