views:

1245

answers:

3

Hi

I have an xml webservice which I'm fetching using PrototypeJS. The xml has the correct content type and is well-formed, and looks like this:

<GetTokenResponse xmlns="http://tempuri.org/"&gt;
    <GetTokenResult>F655100D64F098F0AC33AFF414A4A0D5</GetTokenResult>
</GetTokenResponse>

The AJAX request is completing successfully, and I can access the GetTokenResult node in both IE and FF but can only get the text content of the node in FF. My code is below:

node = transport.responseXML.documentElement.getElementsByTagName('GetTokenResult')[0];
rawToken = (document.all) ? node.innerText : node.textContent;

I've tried innerText and innerHTML, as well as children[0] and a few other chance guesses but IE returns 'undefined' when I access rawToken.

Anyone able to lend a hand? Thanks, Adam

A: 

node = transport.responseXML - this is correct.

You end up with "node" as your XML in string format. Strip the rest. You need to turn the string into an XML document before you can manipulate it directly.

See: http://stackoverflow.com/questions/1290321/convert-string-to-xml-document-in-javascript

or see: http://www.discussweb.com/html-css-javascript-coding-techniques/3308-convert-ordinary-string-into-xml.html

Diodeus
Thanks, but Prototype automatically converts it to XML, hence why I can access the node (but not the node content)
adam
`responseXML`, *if available*, is always a DOM document object, regardless of the ajax library used.
Crescent Fresh
+1  A: 

Try accessing the node value as:

rawToken = node.firstChild.data;

This should work across all modern browsers, as well as IE.

Crescent Fresh
Spot on, thanks!
adam
A: 

To get the text content, use firstChild.nodeValue

Neil