It’s pretty quick-and-dirty, but
this is my first attempt. function xmlToHash(el) { Element.cleanWhitespace(el); if (el.hasAttributes() ||
(el.hasChildNodes() && el.childNodes[0].nodeType == 1)) { var localHash = {}; if (el.hasAttributes
&& el.attributes.length >= 1) {
$A(el.attributes).each(function (attr) { localHash[attr.nodeName] =
attr.nodeValue; }); }
$A(el.childNodes).each(function (el) { xmlToHashElement(localHash, el); }); for (attr in localHash)
{ if
(localHash[attr].length == 1) {
localHash[attr] = localHash[attr][0]; } } return localHash; } else { return el.textContent
|| ''; } } function xmlToHashElement (hash, el) { if (el.nodeType == 2) { hash[el.localName] = [
el.textContent ]; } else {
var key = el.tagName;
if (hash[key]) {
hash[key].push(xmlToHash(el));
}
else {
hash[key] = [ xmlToHash(el) ];
} } } You just do: var hash =
xmlToHash(response.responseXML.documentElement); The tag name of the child nodes become the
keys in the hash, and if there are more than one with the same tag name, it is
an array as the value. It handles attributes as well, with the attribute
name becoming the hash key and the value the value. I imagine if you had
a child node with a tag name that matched the attribute, it would overwrite the
attribute. I didn’t think that was worth handling, as I couldn’t
think of a legitimate scenario where it would be desirable. And it’s recursive, so you can put
nodes within nodes and it will create hashes out of them. I imagine it
doesn’t handle every possible scenario, but it’s a decent start.
You need to load prototype to use this, btw. Greg From:
[EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Tom Riley This might not be practical for you, but the google maps api has an xml
parser that uses a native xml parser if the browser has one and a _javascript_
parser if the browser doesn't. Also, just found http://xmljs.sourceforge.net/ Tom Riley
On 17 Mar 2006, at 16:46,
Greg From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
On Behalf Of Maninder, Singh Maybe this would help you :) Thank you, p.s. I have not written this. I got this from
some site long time back. XMLParser = Class.create(); <*cript language="_javascript_" type="text/_javascript_"> <?xml version="1.0" encoding='euc-kr'?> _______________________________________________ Rails-spinoffs mailing list |
_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs