Newbie question...

N

Nobody

Hi everyone,

I'm new to the group as well as only having a little JS under my belt. I am
familiar with a few other languages but I'm having trouble with some of the
little JS quirks.

I'm attempting to write a (reasonably) generic function to take XML returned
from the XMLHttpRequest object and use DOM functions to insert it. Sounds
easy, right? :)

The XML I am parsing is actually XHTML wrapped in proper XML fashion. It is
not an entire XHTML document, but a fragment which I want to append to the
page. I have validated the XML so I know that's not my issue.

But, I'm stuck. For some reason my function will only return text and image
nodes and I can't figure out why. It traverses the XML tree completely &
correctly, but won't return the node unless it's text, or incorrectly
returns a text node, I can't figure out which. However, the final iteration
does return a div element, which is the desired behaviour. The returned div
element is completely empty.

Here's my function. The error_handler is for later consideration.

function XML2DOM(xml_obj, error_handler)
{
var dom_obj = false;
var child_node;
var child;

for(i in xml_obj.childNodes)
{
child_node = xml_obj.childNodes;
switch(child_node.nodeType)
{
case 1:
//alert(child_node.nodeName);
dom_obj = document.createElement(child_node.nodeName);

child = XML2DOM(child_node, error_handler);
alert(child);
if(child)
{
dom_obj.appendChild(child);
}
break;
case 3:
dom_obj = document.createTextNode(child_node.nodeValue);
break;
case 2:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
alert("Warning: XML2DOM encountered an unknown nodeType: " +
child_node.nodeType);
break;
default:
break;
}
}

return dom_obj;
}


Hopefully that's enough detail. This function is a long way from complete,
and may even be plain dumb, but I want the very basic functionality first.
The warning isn't put in the default switch case section because there's a
bunch of undefined nodes, dunno what that's about.

Thanks,
Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top