Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2004, 07:36 PM   PM User | #1
Skyzyx
Regular Coder

 
Skyzyx's Avatar
 
Join Date: Aug 2002
Location: Silicon Valley, CA
Posts: 980
Thanks: 0
Thanked 0 Times in 0 Posts
Skyzyx is on a distinguished road
RSS Element Parsing Issue

I'm working on developing an RSS/RDF/Atom Parser in JavaScript. I've already successfully implemented complete support for RSS 0.9x and 2.0. So far, so good. However, I've run into two minor problems. One is mentioned here, and one is in another post.

If you have an RSS feed that looks something like this:

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">

	<channel> 
		<title>Skyzyx.com</title>
		<link>http://www.skyzyx.com/</link>
		<description>Advocacy, Evangelism, and Propaganda for Standards-Compliant Design.</description>

		<item>
			<title>SpamMaster Joe</title>
			<link>http://www.skyzyx.com/archives/000160.php</link>
			<description>Joe Lieberman spammed me today! Well, kind of...</description>
		</item>

	</channel>
</rss>
Assuming that I've created a variable to represent the <channel> tag, I could use the following code to parse out the value of the first <description> tag:

Figure 1:
Code:
RSSChannel.getElementsByTagName("description")[0].firstChild.data;
For the sake of naming purposes, let's assume that <item> has a child of <description>. <item> is a child of <channel>, and so is its "brother", <description>. Therefore we have two <description> tags with an Uncle-Nephew relationship. This is how I will explain them.

If the Uncle <description> exists (which it should, since it's a required element, but I digress), then the above code will parse out the value of the Uncle <description>. So far, so good.

Keeping the same assumptions, the following code will parse out the value of the Nephew <description> tag:

Figure 2:
Code:
RSSChannel.getElementsByTagName("description")[0].getElementsByTagName("description")[0].firstChild.data;
Excellent! The problem is this: If the Uncle <description> tag is omitted for whatever reason by the creator of the RSS feed, then the code from Figure 1 will default to the Nephew <description> tag instead of returning false (which is what I've programmed the script to do if the sought-out element does not exist). This simply won't do.

Is there any way (or ideas!) to keep the parsing of elements contained within its own "generation" without trying to parse a nested element of the same name?
__________________

Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.
Skyzyx is offline   Reply With Quote
Old 02-01-2004, 08:01 PM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Code:
getChildrenByTagName(oParent, sTagName){
    var
        i=0,
        aResults=[],
        oTemp;
    while((oTemp=oParent.childNodes.item(i++)))
        if(oTemp.nodeName==sTagName)
            aResults.push(oTemp);
    return aResults;
}
You can do it using DOM2 NodeFilters and either Treewalkers or NodeIterators, or you can use DOM3 XPath, to create dynamic collections. This is far more browser compatible, however.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 02-01-2004, 08:09 PM   PM User | #3
Skyzyx
Regular Coder

 
Skyzyx's Avatar
 
Join Date: Aug 2002
Location: Silicon Valley, CA
Posts: 980
Thanks: 0
Thanked 0 Times in 0 Posts
Skyzyx is on a distinguished road
Quote:
Originally posted by liorean
You can do it using DOM2 NodeFilters and either Treewalkers or NodeIterators, or you can use DOM3 XPath, to create dynamic collections. This is far more browser compatible, however.
Eh?

Thanks for the code. As far as that goes, would I be able to add this to the DOM with, say, a prototype, so that I can use it in the same manner as getElementById or getElementsByTagName?

What would be the recommended syntax for utilizing this as-is?
__________________

Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.
Skyzyx is offline   Reply With Quote
Old 02-01-2004, 08:23 PM   PM User | #4
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Because of bad browser behavior, you shouldn't try to prototype it. Only moz allows prototyping to all the main DOM objects. Opera allows prototyping only to the Node object. (Unless they have added more since I tried it last time, op7.02.) I have no idea how Saf handles it. Iew doesn't, however.

No, instead of using it the old way
Code:
[object DOMElement].getChildrenByTagName([string TagName]);
You should use if as a global (or local) function, use it as-is,
Code:
getChildrenByTagName([object DOMElement], [string TagName]);
It returns an array with the elements. (Note that it's an array, not a collection, so it will not be dynamically updated if you change the DOM, in difference to the regular DOM methods.)
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 02-01-2004, 09:27 PM   PM User | #5
Skyzyx
Regular Coder

 
Skyzyx's Avatar
 
Join Date: Aug 2002
Location: Silicon Valley, CA
Posts: 980
Thanks: 0
Thanked 0 Times in 0 Posts
Skyzyx is on a distinguished road
That makes sense. Thanks!

Quote:
Originally posted by liorean
It returns an array with the elements. (Note that it's an array, not a collection, so it will not be dynamically updated if you change the DOM, in difference to the regular DOM methods.)
Since it's just parsing out an RSS feed, I don't think I'll need to update any of the document, so this should work fine.
__________________

Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.
Skyzyx is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:00 AM.


Advertisement
Log in to turn off these ads.