[clug-progsig] Javascript, again.
John E. Jardine
john_e_jardine at spamcop.net
Tue Oct 10 01:29:21 PDT 2006
Hey Shawn,
You're up late again :)
You never did send me those log files.
Cheers,
J.J.
----- Original Message -----
From: "Shawn" <sgrover at open2space.com>
To: "CLUG PROGSIG" <clug-progsig at clug.ca>
Sent: Tuesday, October 10, 2006 10:23 AM
Subject: Re: [clug-progsig] Javascript, again.
> With XML, the natural assumption is that you can do something like this:
>
> var myRequestor = getXMLHTTPrequestObj();
> . . .
> var myXML = myRequester.responseXML;
>
> And then use the xml elements normally.
>
> However, I have found that this does not work, especially with IE.
> Instead, do something like this:
>
> var myXML = myRequester.responseXML.getElmentsByTagName("response");
>
> Now, the myXML variable will represent an array of "response" elements
> (usually only one element, unless you are not requesting the root...).
> Of course "response" would correspond to whatever XML tag names you are
> using.
>
> Using this technique - getElementsByTagName() or getElementById() -
> works fine in IE and FF. In IE, it eliminates half the response
> problems. (This threw me for about a week until I noticed the subtle
> difference in approaches...)
>
> HTH
>
> Shawn
>
> Nick Wiltshire wrote:
>> If I am ever able to write javascript it might be a miracle. It seems as
>> though I can't do the most trivial thing.
>>
>> 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 (specifically a div) 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[i];
>> 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
>
>
>
>
> _______________________________________________
> clug-progsig mailing list
> clug-progsig at clug.ca
> http://clug.ca/mailman/listinfo/clug-progsig_clug.ca
More information about the clug-progsig
mailing list