32 Replies Latest reply on Nov 22, 2006 10:26 AM by sbryner

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

    sbryner Level 1
      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.
        • 1. Re: for .... for.... if      loop
          kglad Adobe Community Professional & MVP
          you probably need to strip white space from myNode. check its length against valueToSearchFor's length to confirm.
          • 2. Re: for .... for.... if      loop
            sbryner Level 1
            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,

            • 3. Re: for .... for.... if      loop
              kglad Adobe Community Professional & MVP
              where's valueToSearchFor defined and does it have trailing blanks or carriage returns?
              • 4. Re: for .... for.... if      loop
                sbryner Level 1
                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.

                • 5. Re: for .... for.... if      loop
                  sbryner Level 1
                  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[j].childNodes; is it because of some array issue?

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

                  sky
                  • 6. Re: for .... for.... if      loop
                    sbryner Level 1
                    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???
                    • 7. Re: for .... for.... if      loop
                      kglad Adobe Community Professional & MVP
                      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 ";
                      • 8. Re: for .... for.... if      loop
                        sbryner Level 1
                        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?

                        • 9. Re: for .... for.... if      loop
                          kglad Adobe Community Professional & MVP
                          what's the output panel show with the following trace() added:

                          • 10. Re: for .... for.... if      loop
                            sbryner Level 1
                            Here is what I get.

                            sky

                            • 11. Re: for .... for.... if      loop
                              kglad Adobe Community Professional & MVP
                              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.
                              • 12. for .... for.... if      loop
                                sbryner Level 1
                                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?
                                • 13. Re: for .... for.... if      loop
                                  kglad Adobe Community Professional & MVP
                                  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.
                                  • 14. Re: for .... for.... if      loop
                                    sbryner Level 1
                                    ok... now I'm lost.. I've looked at kirupa.com and google search to find out how to create:

                                    "bracket notation to resolve the string to an object "

                                    can you give me an example I can "pick apart/study" line by line so I can understand this more (I'd like to be able to truely understand it not just use it for this project) or send a site that I can go to so I know exactly what I'm trying to do?

                                    I know just changing the "new String" to "new Object" won't work in the code posted above.

                                    Hope I'm not frustrating you.
                                    • 16. Re: for .... for.... if      loop
                                      sbryner Level 1
                                      Still doesn't trace inside the if statement?

                                      I can trace to the line above the if statment but does not go inside the statement...
                                      do you know why this might be?
                                      • 17. Re: for .... for.... if      loop
                                        kglad Adobe Community Professional & MVP
                                        instead of using tl, use this:

                                        • 18. Re: for .... for.... if      loop
                                          sbryner Level 1
                                          tried that before I replied. no go on that either
                                          • 19. Re: for .... for.... if      loop
                                            kglad Adobe Community Professional & MVP
                                            lol, ok. how about:

                                            • 20. Re: for .... for.... if      loop
                                              sbryner Level 1
                                              still nada!

                                              could it be something to do with the component value or heck I don't get it.
                                              • 21. Re: for .... for.... if      loop
                                                sbryner Level 1
                                                also... to declare the variable an object you only need to add brackets? that's it?

                                                [ stringIsNowAnObject ] ?
                                                • 22. Re: for .... for.... if      loop
                                                  kglad Adobe Community Professional & MVP
                                                  an object needs to already exist as a child of some other object (usually a timeline, but not necessarily). you can then use the fact that children of objects are stored, by flash, in an array of the object.

                                                  myNode is an array, correct? and when you trace(myNode) you see one value because myNode is an array containing one element. maybe you should be testing myNode[0] against valueToSearchFor. lol, i'm bound to get it sooner or later.
                                                  • 23. Re: for .... for.... if      loop
                                                    sbryner Level 1
                                                    Just don't give up on me. I was once told by another member: kglad > all
                                                    so you helping me feels like one of the greats helping me.


                                                    I've tried the last three codes posted "tl=this" removing the "tl=this" and using this plus the last "if(nodesToLookIn[ i ].childNodes[ j ] [valueToSearchFor] == myNode)"

                                                    thanks for clarifying a little more for me in your last reply.
                                                    • 24. Re: for .... for.... if      loop
                                                      kglad Adobe Community Professional & MVP
                                                      well, in my last reply i started explaining why the situation you were describing couldn't happen when i typed myself towards, what i think is, the solution.

                                                      try:

                                                      • 25. Re: for .... for.... if      loop
                                                        sbryner Level 1
                                                        Hi, Neither code worked. I'm posting all my info so you may see something I'm missing elsewhere.

                                                        The only other thing is two components on the stage, a button component on the stage and a
                                                        comboBox component .

                                                        The comboBox has a value of "p" and a data value of "p".
                                                        This is just so you may see a step I've not taken.

                                                        can you see anthing I must do first other than the code posted?

                                                        sky

                                                        Here is my xml file:

                                                        <?xml version="1.0" ?>
                                                        <family>
                                                        <person>
                                                        <myname>p</myname>
                                                        <comments>That's my ol' man!</comments>
                                                        </person>
                                                        <person>
                                                        <myname>Silly Sally</myname>
                                                        <comments>She's so funny</comments>
                                                        </family>



                                                        • 26. Re: for .... for.... if      loop
                                                          sbryner Level 1
                                                          I'm still struggling with this Object and String problem

                                                          I'm getting the myNodes = Object & valueToSearchFor = String

                                                          I'm wanting them to be the same correct so I can check for equality?
                                                          • 27. Re: for .... for.... if      loop
                                                            kglad Adobe Community Professional & MVP
                                                            that's not a well-formed xml file. and second, you need to check the nodeValue of myNode[k] against valueToSearchFor:

                                                            • 28. for .... for.... if      loop
                                                              sbryner Level 1
                                                              THE CODE WORKS. I was using the xml file to just to test this project.

                                                              One more question. I've been reading about searching XML on Kirupa.com
                                                              and I want to add the nodeValue[s] of my search into the myResults Array.
                                                              I thought everytime I entered into the if statment I could use:

                                                              myResults.push(this(nodesToLookIn[ i ].childNodes)); // I know this code is wrong but something like it is what I'm trying to do.

                                                              this would add all the childNodes for that person into an array (myResults) as elements. My ultimate goal is something like this with the myResult:

                                                              myResult = [{myName:"p", comments: "thats my ol man";}
                                                              {myName:"p", comments:"hey man"}];

                                                              or something along those lines so I could load each item from the myResults array into text fields and image names into mc's.

                                                              Am I out there on this idea or is it possible? If so, am I close to doing this?
                                                              • 29. Re: for .... for.... if      loop
                                                                kglad Adobe Community Professional & MVP
                                                                here's your code with some comments so you might better see what your accessing:

                                                                • 30. Re: for .... for.... if      loop
                                                                  sbryner Level 1
                                                                  Thanks Kglad for all the help.

                                                                  I've been working on my own little project for awhile and it went from searching an AS Array and placing the elements into seperate MC's which inside them load all the elements for each "valueToSearchFor" into dynamic text boxes and image paths load into mc's to display pictures.

                                                                  I'm back on track now that you've helped me. I've got a ways to go. I have a prototype I used before with the AS associates array but now I don't think I can use it. I'm going to try another avenue instead of the Array.prototype for this.

                                                                  I'm going to try using :

                                                                  loadName= myResults[ i ] [ 0 ];
                                                                  loadComment = myResults[ i ] [ 1 ];

                                                                  like I used the Array.prototype to create this before:

                                                                  email_txt.text = searchResult [ i ].email;

                                                                  if you see an automatic problem with this please let me know.
                                                                  else, I'm going to venture back into the relm of "self searching" to
                                                                  continue my little project.

                                                                  I really appreciate your help and time... Man, couldn't have done it without ya.
                                                                  I've been working on this problem for 2 months now.

                                                                  Thanks a bunch, and I'm sure...wait, I KNOW, I'll have more questions in the future I hope you will help with.

                                                                  thanks for everything.
                                                                  • 31. Re: for .... for.... if      loop
                                                                    kglad Adobe Community Professional & MVP
                                                                    you're welcome.

                                                                    p.s. it's easy to change a prototype method into a function. that function can probably be easily adapted to working on 2-d arrays like myResults.

                                                                    if you want to try going that route, post the code to your Array.prototype method.
                                                                    • 32. Re: for .... for.... if      loop
                                                                      sbryner Level 1
                                                                      Thanks again,

                                                                      I used it (pre XML) like this:
                                                                      my "search.swf" loads into a "main.swf" and the result loads "searchResults.swf " into the "main.swf" onRelease...

                                                                      searchCriteria in my code = comboBox.value
                                                                      searchBy = a string // set when "onRelease" was executed to tell what element in the array to look for.

                                                                      I hope you can picture what I'm doing here.
                                                                      At least that was before XML was introduced into my life.

                                                                      Now I'm trying to get the searched value from my comboBox.value, search the XML file for the value
                                                                      once found it loads it into the mc's of my returnSearch.swf.

                                                                      anyway you probably don't need to know all that but here is my prototype array and how it is used pre XML