Prototype ajax + xml response

Hi,
I am looking for a solution to use ajax.request object.
Return response text will be in xml format and I like to know how to
parse that xml information, so that I can put those info into the “span”
or “div” tags of my html page. Is there any easy way to parse that xml
info using prototype library?

Thanks,
kevin.

Hi Kevin,

I lifted the following code:

onSaveComplete: function(response)
{
var ajaxResponse = Try.these(
function() { return new
DOMParser().parseFromString(response.responseText,
‘text/xml’); },
function() { var xmldom = new ActiveXObject(‘Microsoft.XMLDOM’);
xmldom.loadXML(response.responseText); return xmldom; }
);

var personid =
ajaxResponse.getElementsByTagName(‘personid’)[0].firstChild.nodeValue;
}

From here:
http://weblogs.macromedia.com/mesh/archives/2006/01/parsing_xml_in.cfm

Hope this helps!
Daniel

On Wed, Feb 22, 2006 at 07:13:16PM -1000, Daniel H. wrote:
} Hi Kevin,
}
} I lifted the following code:
}
} onSaveComplete: function(response)
} {
} var ajaxResponse = Try.these(
} function() { return new
DOMParser().parseFromString(response.responseText,
} ‘text/xml’); },
} function() { var xmldom = new ActiveXObject(‘Microsoft.XMLDOM’);
} xmldom.loadXML(response.responseText); return xmldom; }
} );
}
} var personid =
} ajaxResponse.getElementsByTagName(‘personid’)[0].firstChild.nodeValue;
} }
}
} >From here:
} http://weblogs.macromedia.com/mesh/archives/2006/01/parsing_xml_in.cfm

Er, what’s wrong with response.responseXML? A parsed XML tree is already
available from the XMLHttpRequest object.

} Hope this helps!
} Daniel
–Greg

Daniel H. wrote:

Hi Kevin,

I lifted the following code:

onSaveComplete: function(response)
{
var ajaxResponse = Try.these(
function() { return new
DOMParser().parseFromString(response.responseText,
‘text/xml’); },
function() { var xmldom = new ActiveXObject(‘Microsoft.XMLDOM’);
xmldom.loadXML(response.responseText); return xmldom; }
);

var personid =
ajaxResponse.getElementsByTagName(‘personid’)[0].firstChild.nodeValue;
}

From here:
http://weblogs.macromedia.com/mesh/archives/2006/01/parsing_xml_in.cfm

Hope this helps!
Daniel

Thanks a lot Daniel. I appreciate it. I will try this.
Kevin

Gregory S. wrote:

Er, what’s wrong with response.responseXML? A parsed XML tree is already
available from the XMLHttpRequest object.

} Hope this helps!
} Daniel
–Greg

When I tried this (on FireFox IIRC) the responseXML property was null.
I read somewhere that this can happen if the mime type isn’t set to
“text/xml”, and I’ve concluded that prototype doesn’t have a way of
setting this.

I’d love to be able to rely on .responseXML - do you know how to ensure
it will be set?

Tony S.

On Fri, Feb 24, 2006 at 02:02:51AM +0100, Tony Stevens wrote:
} Gregory S. wrote:
} >
} > Er, what’s wrong with response.responseXML? A parsed XML tree is
} > already available from the XMLHttpRequest object.
} >
} > } Hope this helps!
} > } Daniel
} > --Greg
}
} When I tried this (on FireFox IIRC) the responseXML property was null.
} I read somewhere that this can happen if the mime type isn’t set to
} “text/xml”, and I’ve concluded that prototype doesn’t have a way of
} setting this.
}
} I’d love to be able to rely on .responseXML - do you know how to
ensure
} it will be set?

Well, the MIME type needs to be set properly. I haven’t touched the
prototype library, but my understanding is that it is solely on the
JavaScript side, not the Ruby side. In your Rails action you need to set
the MIME type.

If I am wrong and prototype is on the Rails side as well., modify it.
You
have the source, after all. Make sure it is setting the MIME type.

} Tony S.
–Greg

Gregory S. wrote:

Well, the MIME type needs to be set properly. I haven’t touched the
prototype library, but my understanding is that it is solely on the
JavaScript side, not the Ruby side. In your Rails action you need to set
the MIME type.

If I am wrong and prototype is on the Rails side as well., modify it.
You
have the source, after all. Make sure it is setting the MIME type.

} Tony S.
–Greg

Thankyou! That was just the help I needed.

Once I realised the problem was that the rails action wasn’t saying that
it was sending XML it gave me a place to start looking. After a while I
figured out how to set the response header.

It’s all about how you look at it …

Tony S.