Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    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.
    Last edited by liorean; 11-14-2004 at 06:27 AM.
    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

  • #3
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •