8 Replies - 552 Views - Last Post: 15 April 2011 - 07:46 AM

#1 kbscores  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-February 09

xmlDom/Javascript Question

Posted 13 April 2011 - 04:41 AM

I'm still pretty new to javascript and dom, but have been coding with other languages for a while.

When I try to compare a string to a .nodeValue it always fails, even if they are same. From what I've read typecasting is automatic in javascript or is this incorrect?

For instance

var myGreet = "Hello";
var myFrag = myXML.lastChild.firstChild.firstChild.childNodes[0].nodeValue;  //returns Hello

if(myGreet == myFrag){  // always returns false
   alert(myFrag);
}



Is This A Good Question/Topic? 0
  • +

Replies To: xmlDom/Javascript Question

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: xmlDom/Javascript Question

Posted 13 April 2011 - 05:35 AM

Are you sure that myFrag contains a string with the value "Hello" ? What happens if you alert(myFrag) ?
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3393
  • View blog
  • Posts: 9,591
  • Joined: 08-June 10

Re: xmlDom/Javascript Question

Posted 13 April 2011 - 05:43 AM

it might be that myFrag is not what you expect it to be. there are slight differences in the node handling in browsers.
Was This Post Helpful? 0
  • +
  • -

#4 kbscores  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-February 09

Re: xmlDom/Javascript Question

Posted 13 April 2011 - 05:53 AM

Is there a way to compare myFrag to a string?

myFrag will display fine within alert, comparison is issue.

Most likely because they aren't same type, but since javascript automatically handles types...I'm running into a wall.

This post has been edited by kbscores: 13 April 2011 - 05:54 AM

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3393
  • View blog
  • Posts: 9,591
  • Joined: 08-June 10

Re: xmlDom/Javascript Question

Posted 13 April 2011 - 06:53 AM

what document is myXML? maybe we can spot something unusual there, otherwise itís futile to discuss what myFrag is or ainít.
Was This Post Helpful? 0
  • +
  • -

#6 kbscores  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-February 09

Re: xmlDom/Javascript Question

Posted 14 April 2011 - 06:24 AM

I feel completely lost. How does xml document have anything to do with how .nodeName compares to a string?
Does it have to do with encoding of xml document?

<?xml version="1.0" encoding="UTF-8" ?> 
<myroot>
<mygreetings>
  <greetin>Hello</greetin> 
  <question>How are you?</question> 
  <greetout>Goodbye</greetout> 
</mygreetings>
<mygreetings>
  <greetin>Hola</greetin> 
  <question>¿Cómo estás?</question> 
  <greetout>Adiós</greetout> 
</mygreetings>
<mygreetings>
  <greetin>Hallo</greetin> 
  <question>Wie geht es Ihnen?</question> 
  <greetout>Auf Wiedersehen</greetout> 
</mygreetings>
</myroot>


This post has been edited by kbscores: 14 April 2011 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3393
  • View blog
  • Posts: 9,591
  • Joined: 08-June 10

Re: xmlDom/Javascript Question

Posted 14 April 2011 - 01:27 PM

it has something to do, whether you get the right node. without testing I’d say you have one firstChild too much. the root node of XML is usually referred by XML.documentElement and (depending on the browser), even a line break between tags counts as separate (Text) Node.

try myXML.getElementsByTagName("greetin")

This post has been edited by Dormilich: 14 April 2011 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 kbscores  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-February 09

Re: xmlDom/Javascript Question

Posted 15 April 2011 - 07:27 AM

var myGreet = "Hello";  
var myFrag = myXML.getElementsByTagName("greetin");  //returns Hello  

for(var i = 0; i < myFrag.length; i++){
   if(myGreet == myFrag[i].childNodes[0].nodeValue){  // always returns false  
      alert(myFrag[i].nextSibling.childNodes[0].nodeValue);  //Should Alert How Are you?
   }
} 



This was supposed to be a theortical situation in which I search for one piece of data that I have to get another related piece.

However, because my if statement always returns false. My dilemma is .nodeValue always returns false when comparing to a string.

My question is how do I accurately compare a .nodeValue to a string?
Was This Post Helpful? 0
  • +
  • -

#9 kbscores  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 01-February 09

Re: xmlDom/Javascript Question

Posted 15 April 2011 - 07:46 AM

Figured it out. When an xml file is generated it adds a space to end of value. because it doesn't ignore white space it causes two elements not to be equal. Thank you for your time I do appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1