Skip to main content
sbryner
Inspiring
November 14, 2006
Question

for .... for.... if loop

  • November 14, 2006
  • 19 replies
  • 1831 views
hello,

I'm trying to search an XML file checking the childNodes of said file.
The problem is I have a function that looks like this:

going into the if statement the to variables I want to check to see if they are equal ARE EQUAL
but it does not go pass the if portion of the function

Why?

please help.
This topic has been closed for replies.

19 replies

kglad
Community Expert
Community Expert
November 16, 2006
no, they're not. the string constructor returns the primitive value (if it exists) of the object. when it doesn't exist the object is returned.

so, you can't convert to a generic object to a string. you'll have to use my 2nd suggestion. convert the string into an object and compare the two objects.
kglad
Community Expert
Community Expert
November 15, 2006
and can you see why they're different? one is an object and the other is a string. to remedy you can test a string representation of the object (use the toString() method of objects) against the string or use bracket notation to resolve the string to an object and test against the object.
sbryner
sbrynerAuthor
Inspiring
November 15, 2006
ok... I think I figured out the conversion to object problem.

Now both read myNewNode and myNewValue are both objects containing "p"

But it still will not read the if statment... see attached code.
Am I close?
kglad
Community Expert
Community Expert
November 15, 2006
if valueToSearchFor="p" and myNode="p", then your if-statement will execute.

the length of a string variable like valueToSearchFor and myNode is the number of characters in the string. when you use a trace() function that shows both variables appear to be the same but an if-statement fails to execute it's usually because of white space. eg,

myNode="p ";
sbryner
sbrynerAuthor
Inspiring
November 15, 2006
that's what I thought if they == to each other then the if statement should execute.

I've attached the code minus loading the xml file into an XML object.

I've used the comboBox component and the data types I've tried were "Tom" and "Sally"
Now, when I search inside my XML object for "Tom" or "Sally in the childNodes I can trace
both equal the same but it still does not go into the if statement.

why?

I've even changed both the file to include <name>p</name> and myComp (comboBox instanceName) with the value "p" without whitespace both have a length =1 and still no if statement access.

Is there something wrong with my code?

kglad
Community Expert
Community Expert
November 15, 2006
what's the output panel show with the following trace() added:

sbryner
sbrynerAuthor
Inspiring
November 14, 2006
does myNode.length = 1 because it is searching at that second only one childNodes?

Also,

I've changed my valueToSearchFor to equal "p" so I have a length of 1, same as the myNode.length.... still nothing. I don't understand why it will not go into the if statment inside my searchXML function.

why o' why???
sbryner
sbrynerAuthor
Inspiring
November 14, 2006
I've figured out the length is referring to the the length of characters in
valueToSearchFor.
(ie...
valueToSearchFor ="Tom" ; // length = 3
valueToSearchFor ="Cliff"; // length =5
)

myNode.length =1 // because why?

var myNode = myXML .childNodes.childNodes; is it because of some array issue?

still why would this matter (the length of each being the same)?

sky
sbryner
sbrynerAuthor
Inspiring
November 14, 2006
the valueToSearchFor is
defined before the function as

var valueToSearchFor = myComponent.value;

so it's a component value that I want to use onRelease of a btn
to search the XMLNodes for.

It doesn't have blanks after it or a carriage return that I know of it's just a value I set in the parameters of the myComponent comboBox.

kglad
Community Expert
Community Expert
November 14, 2006
where's valueToSearchFor defined and does it have trailing blanks or carriage returns?
sbryner
sbrynerAuthor
Inspiring
November 14, 2006
I checked it and my valueToSearchFor.length = 3 & myNode.length =1

what do you mean by strip the white space?

do I

valueToSearchFor.ignoreWhite = true;
or
myNode.ignoreWhite = true;

I dont' understand what actually is happening then.
Can you explain what is happening?

thanks,

kglad
Community Expert
Community Expert
November 14, 2006
you probably need to strip white space from myNode. check its length against valueToSearchFor's length to confirm.