XML DOM Help needed
August 30, 2007 9:25 PM Subscribe
XML DOM question: Help a newbie. I can't figure out how to do something that seems so simple. I'm writing an extremely simple RSS reader that creates a select control. Each option element needs only to be filled with the values of the "link" and "title" elements but I can't figure out how to reference them by name.
In the RSS its basicaly only the "item" elements i'm interested in and "link" and "title" are two elements which are its children that I need to fetch the values for
Heres my initial code (ASP):
-------------------------------------------------------------------------
Set Root=source.documentElement
Set PodcastList=Root.getElementsByTagName("item")
For Each Podcast In PodcastList
Set Podcast_URL = [HOW?]
Set Podcast_title = [HOW?]
Response.write("<option value=""" & Podcast_URL.text & """>" & Podcast_URL.text & "</option>")
Next
--------------------------------------------------------------------------
What I can't figure out is how to request just the element i want by its name without using getelementsbyname() since that returns a list and there seem to be properties available to return single elements like firstChild or lastChild so it seems logical that there would be a method to do it for a single named element. I know xpath is probably what I need but i'm not at the learning stage where I know how to incorporate that into my code.
Thanks for your help!
In the RSS its basicaly only the "item" elements i'm interested in and "link" and "title" are two elements which are its children that I need to fetch the values for
Heres my initial code (ASP):
-------------------------------------------------------------------------
Set Root=source.documentElement
Set PodcastList=Root.getElementsByTagName("item")
For Each Podcast In PodcastList
Set Podcast_URL = [HOW?]
Set Podcast_title = [HOW?]
Response.write("<option value=""" & Podcast_URL.text & """>" & Podcast_URL.text & "</option>")
Next
--------------------------------------------------------------------------
What I can't figure out is how to request just the element i want by its name without using getelementsbyname() since that returns a list and there seem to be properties available to return single elements like firstChild or lastChild so it seems logical that there would be a method to do it for a single named element. I know xpath is probably what I need but i'm not at the learning stage where I know how to incorporate that into my code.
Thanks for your help!
You can also call getElementsByTagName as a method on a DOM node to restrict its scope to the contents of that node. In other words if you want to iterate over each item element and work with the link and title elements inside them, you could do (in JavaScript):
posted by letourneau at 4:43 AM on August 31, 2007
var items = document.getElementsByTagName("item"); for (var i = 0, len = items.length; i < len; i++) {br> var link = items[i].getElementsByTagName("link")[0]; var title = items[i].getElementsByTagName("title")[0]; // Do what you need to do here with link and title }>(Note the [0] array index notation there which assigns just the first node returned into the variable, since the method returns an array of nodes.) If those nodes contain just text, you can look at their first child elements respectively (which will be text nodes) and use the nodeValue attribute to get the actual text contents.
posted by letourneau at 4:43 AM on August 31, 2007
I don't know what the heck that br> is doing on the second line of my code sample, but you get the picture.
posted by letourneau at 4:44 AM on August 31, 2007
posted by letourneau at 4:44 AM on August 31, 2007
Response by poster: Thanks, this is now working for me! I used the getelementsbyname method and am grabbing the first index position.
I was surprised though that there was no other way to do this? It seems that this should be possible somehow considering that xpath contains all these capabilities. Something about having to read all the child elements called "link" into an array even though I know there is only one in the schema seems inefficient.
posted by postergeist at 7:50 AM on August 31, 2007
I was surprised though that there was no other way to do this? It seems that this should be possible somehow considering that xpath contains all these capabilities. Something about having to read all the child elements called "link" into an array even though I know there is only one in the schema seems inefficient.
posted by postergeist at 7:50 AM on August 31, 2007
This thread is closed to new comments.
so if x=getelementsbyname("myname");
then x[0] gives you the element you want.
posted by juv3nal at 11:24 PM on August 30, 2007