LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-01-2006, 09:22 PM   #1
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
Question JavaScript:: alert(node) shows null, but node != null


Hello; I'm trying to do extract all the information contained within an XML element so I can put it into a dynamic web page. The best way I could think of to do this is using a recursive function. So here's what I've got so far:
Code:
function recParse(node) {
    s = "";
    n = node.firstChild;
    // alert(n);
    while (n != null) {
        s += n.nodeValue;
        s += recParse(n);
        n = n.nextSibling;
    }
    return s;
}
It would seem to me that this would do exactly what I need, except it doesn't. The test data I'm using is something like this:
Code:
<text>This is some text <code>with a code element</code> inside of it.</text>
When it goes to pick up the code element (that's the text element's first child), Firefox tells me for the line n.nodeValue that "n has no properties". When I uncomment that alert line, the warning box says "null"; I'm assuming that means n == null. But that can't be the case because it's passing through the while() condition and trying to execute n.nodeValue. So I'm fairly confused. JavaScript is not my forte, so any help anybody can offer will be greatly appreciated. Or if there's a less complicated way to do this, that'd be even better. Essentially, I need to take the entirety of that text element above, and put it inside a span element. As always, thanks a bunch.
 
Old 05-01-2006, 09:51 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Original Poster
Rep: Reputation: 40
OK, I feel bad now for posting back so quickly, but I guess the problem was my incorrect way of thinking about how SGML is handled in JavaScript. In case anybody else has the same problem, this is the code I wrote to handle it:
Code:
function iterParse(node) {
    var s = "";
    for (var n = 0; n < node.childNodes.length; n += 1) {
        var child = node.childNodes[n];
        if (child.nodeType == 3) { // text node
            s += child.nodeValue;
        }
        else if (child.nodeType == 1) { // element node
            s += "<" + child.nodeName + " ";
            for (var a = 0; a < child.attributes.length; a += 1) {
                s += child.attributes[a].nodeName + "='";
                s += child.attributes[a].nodeValue + "' ";
            }
            s += ">";
            s += iterParse(child);
            s += "</" + child.nodeName + ">";
        }
    }
    return s;
}
Although the problem is now largely solved, I still welcome any opinions anybody may have about this method, or if there's an easier way to do it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fstab-sync: error: libhal_ctx_init_direct: (null): (null) rpz Linux - Hardware 1 11-01-2005 05:42 AM
Javascript error "is null or not an object" apt Programming 2 03-28-2005 06:14 AM
Linux i-node chipnuke Linux - Software 3 12-28-2004 12:45 PM
Spam node Vyeperman *BSD 10 10-03-2003 08:31 AM
Update Node Name buzzbuzz Linux - General 20 02-06-2003 11:25 AM


All times are GMT -5. The time now is 04:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration