0

I'm trying yo read some specific data from an XML by the use of a IF sentence, it doesn't seem to work though. My code:

tipo=(x[i].getElementsByTagName("tipo")[0].childNodes[0].nodeValue);
subcategoria=(x[i].getElementsByTagName("subcategoria")[0].childNodes[0].nodeValue);
    if (tipo=="comer" && subcategoria=="bodegas") {
        Bodegas.push(x[i].getElementsByTagName("nombre")[0].childNodes[0].nodeValue)

    };

I've been debuging via alerts, and I can tell that tipo & subcategoria values are "comer" and "bodegas" for sure, but when I put an alert inside the if sentence it doesn't run, so I guess the comparison is not properly done. Any ideas? Thanks!!

5
  • Have you tried alerting (tipo == "comer" ? "YES" : "NO") and (subcategoria == "bodegas" ? "YES" : "NO") to see if it's both of the variables that are going wrong? Commented Feb 15, 2013 at 11:19
  • I've just tried, both statements return NO, wtf Commented Feb 15, 2013 at 11:25
  • You can share your code on JsFiddler ? Commented Feb 15, 2013 at 11:27
  • 2
    Are you sure that the actual string isn't comer*[SPACE]* or *[SPACE]*comer etc? There isn't any spaces or something? Try to use alert("'" + tipo + "'"); and see if there's anything between the ' that shouldn't be there. Commented Feb 15, 2013 at 11:29
  • Okey,that was the problem, the xml file was filled with white spaces, thank you so much man. Commented Feb 15, 2013 at 12:13

1 Answer 1

2

On comparing Strings always call the trim() and toLowerCase() on both sides of the query to ensure there are no casing or spaces issues.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.