Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


LinkBack Search this Thread
Old 05-01-2006, 09:22 PM   #1
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:
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:
<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
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:
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.


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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 07:40 AM.

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