...

View Full Version : Working with XML and retrieving nodes



fci
11-14-2004, 04:51 AM
function Xml_feed(file)
{
this.load = function()
{
var txt, str, title;
var threads = xml.getElementsByTagName('thread');
var len = threads.length;
var box = document.getElementById('box-a');
while (len-->0) {
txt = document.createElement('button');
title = threads[len].getElementsByTagName('title');
str = threads[len].getElementsByTagName('author');
txt.setAttribute('label', title[0].childNodes[0].nodeValue + ' by ' + str[0].childNodes[0].nodeValue);
box.appendChild(txt);
}
};

var xml = document.implementation.createDocument("","",null);
xml.onload = this.load;
try {
xml.load(file);
} catch (e) {
alert('There was a problem loading the XML file');
}
}
var feed = new Xml_feed('blah.xml');


in the red:
Is that how I should be retrieving the value from a child node? (the getElementsByTagName)..

in the blue:
I tried doing this.xml but had problems with doing that, i.e,

this.xml = document.implementation.createDocument("","",null);
this.xml.onload = this.xml.load;
try {
this.xml.load(file);
} catch (e) {
alert('There was a problem loading the XML file');
}
I'll guess that text in the red is why..
btw, this was in an XUL app.. not sure how much that would matter though..

liorean
11-14-2004, 05:45 AM
in the red:
Is that how I should be retrieving the value from a child node? (the getElementsByTagName)..
No. That is how you retrieve the node itself. If the node contains a text node, you can access [object node].firstChild.data if you want to read out the value of it.

in the blue
Those are entirely different things. The blue example sets the onload listener of the document object to the value of the load member of the Xml_feed object. The red alternative sets the onload listener of the document object to the value of load member of the document object.

fci
11-14-2004, 06:17 AM
No. That is how you retrieve the node itself. If the node contains a text node, you can access [object node].firstChild.data if you want to read out the value of it.
mmm. I'm not all up on the terminology. My main reason for using getElementsByTagName so I could call the node by its tagName... then I could just write a method which would return the value based on tagName, so I would presume getElementsByTagName is slower then ?.. and for reference the XML file I was using was a copy of this:
http://www.codingforums.com/external.php?type=XML
edit: just fiddled more with what you said so I grasp that better(yayness.).
I haven't really worked with XML, is there usually a rigid set of nodes and the code must adapt to that? which is what brought me to the idea of using getElementsByTagName since I would be able to make it more 'dynamic' .. unless there is a major performance hit by doing it that way...


Those are entirely different things. The blue example sets the onload listener of the document object to the value load memeber of the Xml_feed object. The red alternative sets the onload listener of the document object to the load property of the document object.
mmmm, actually, it was a typo when I was showing the example.
When I do "this.xml.onload = this.load;" it doesn't work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum