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 11-14-2004, 04:51 AM   PM User | #1
fci
Senior Coder

 
Join Date: Aug 2004
Location: Twin Cities
Posts: 1,345
Thanks: 0
Thanked 0 Times in 0 Posts
fci is an unknown quantity at this point
Working with XML and retrieving nodes

Code:
        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,
Code:
            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..

Last edited by fci; 11-14-2004 at 04:53 AM..
fci is offline   Reply With Quote
Old 11-14-2004, 05:45 AM   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
Quote:
Originally Posted by fci
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.
Quote:
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.
__________________
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

Last edited by liorean; 11-14-2004 at 06:27 AM..
liorean is offline   Reply With Quote
Old 11-14-2004, 06:17 AM   PM User | #3
fci
Senior Coder

 
Join Date: Aug 2004
Location: Twin Cities
Posts: 1,345
Thanks: 0
Thanked 0 Times in 0 Posts
fci is an unknown quantity at this point
Quote:
Originally Posted by liorean
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...

Quote:
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.

Last edited by fci; 11-14-2004 at 06:41 AM..
fci 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 03:39 PM.


Advertisement
Log in to turn off these ads.