Closed Thread Icon

Topic awaiting preservation: javascript - xml - nodeValue? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=27002" title="Pages that link to Topic awaiting preservation: javascript - xml - nodeValue? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: javascript - xml - nodeValue? <span class="small">(Page 1 of 1)</span>\

 
Lord_Fukutoku
Paranoid (IV) Inmate

From: San Antonio
Insane since: Jul 2002

posted posted 11-11-2005 21:55

Hopefully this is just something easy that I can't find...

I have an xml document, something like:

code:
<points>
  <point>
    <id>first</id>
    <lat>29</lat>
    <lon>-98</lon>
  </point>
  .
  .
  .
</points>



And I'm trying to traverse it in javascript, with this:

code:
var request = GXmlHttp.create();
	request.open("GET", "PointsXml.xml", true);
	request.onreadystatechange = function() {
	  if (request.readyState == 4) {
	  
	    //alert(request.responseText);
	  	
	    var xmlDoc = request.responseXML;
	    
	    //alert("Root node: " + xmlDoc.documentElement.nodeName);
	    
	    var pointNodes = xmlDoc.documentElement.getElementsByTagName("point");
	    //alert("Number of point nodes: " + pointNodes.length);
	    
	    for ( var i = 0; i < pointNodes.length; i++ ) {
	      	var pointNodeChildren = pointNodes.item(i).childNodes;
	      	
	      	var childValues = "";
	      	for( var j = 0; j < pointNodeChildren.length; j++ ) {
                    childValues += j + ". " + pointNodeChildren[j].nodeName + ": " + pointNodeChildren[j].nodeValue + "\r\n";
	        }
		alert(childValues);
             }
	  }
	}
	request.send(null);



It displays:
0. #text:
1. id: null
2. #text
3. lat: null
4. #text:
5. lon: null
6. #text:


According to: http://developer.apple.com/internet/webcontent/dom2i.html

quote:
<span id="weather">Partly cloudy, scattered showers, high 67</span>

The code above is comprised of two separate nodes. The <SPAN> tag is an element node, with a single ID attribute. The text inside the SPAN is in fact a separate text node. Being only text, it has no attributes or children.

Elements and text nodes share some common properties:

* nodeType: This property holds a numeric value that corresponds to the type of node. Elements have a nodeType value of 1, while text nodes have a nodeType value of 3. This is useful for identifying the type of a particular node in an operation where several nodes of unknown type need to be examined.
* nodeName: This property holds a string that, like nodeType, corresponds to the type of node. All text nodes have the string ?#text? as the value of nodeName. For elements, nodeName contains the name of the element tag. Thus, an HTML image tag would have a nodeName of ?IMG.?
* nodeValue: This property holds the value of the node, if any. Elements have a nodeValue of null. Text nodes have a nodeValue that is the actual string of text within that node.



So the only thing that isn't really making much sense right now is:
Why is there an extra text node?
And why are all the text nodes' values empty?

If someone wouldn't mind enlightening me a little, I'd appreciate it.
Thanks,
LF

(Edited by Lord_Fukutoku on 11-11-2005 21:59)

liorean
Bipolar (III) Inmate

From: Umeå, Sweden
Insane since: Sep 2004

posted posted 11-11-2005 23:05

It's the whitespace between the tags. The DOM and XML specs don't point in either way being correct, but it's important for things like the CSS white-space property being handled as it should, so the resonable handling of it is to let it stay in the DOM.

Just filter it out when traversing if you need to.

--
var Liorean = {
abode: "http://liorean.web-graphics.com/",
profile: "http://codingforums.com/member.php?u=5798"};

(Edited by liorean on 11-11-2005 23:06)

Lord_Fukutoku
Paranoid (IV) Inmate

From: San Antonio
Insane since: Jul 2002

posted posted 11-11-2005 23:37

It's been awhile since I've done anything web-related, so what you just said all sounded familiar, but it went right by me (probably isn't helping that it's Friday afternoon either).

I took the easy way out and re-built my xml file with no white space anywhere, and it got rid of the "#text" nodes, so now I have:

0. id: null
1. lat: null
2. lon: null

I'm not sure if that was a step forward or backward, but it's something different...

I also found this:

quote:

A problem here: the XML document looks like this:

<emperor>
<name>

which means that Netscape considers the empty text node between emperor and name as the first child of emperor.



Not sure, but I would assume this is a Gecko based browser problem, not specifically Netscape since I'm using FireFx and I was seeing this (maybe?).

Lord_Fukutoku
Paranoid (IV) Inmate

From: San Antonio
Insane since: Jul 2002

posted posted 11-11-2005 23:41

Nevermind. I didn't realize that the text node that it was finding was a child of the element node, so you have to get at it like:

childValues += j + ". " + pointNodeChildren[j].nodeName + ": " + pointNodeChildren[j].firstChild.nodeValue + "\r\n";

« BackwardsOnwards »

Show Forum Drop Down Menu