Updated code:
XMLDoc = Class.create();
Object.extend(XMLDoc.prototype, {
  initialize: function (xmlDoc) {
    this.element = xmlDoc;
  },
  asHash: function () {
    if (! this._xmlHash) {
      this._xmlHash = this._nodeToHash(this.element);
    }
    return this._xmlHash;
  },
  _nodeToHash: function (node) {
          Element.cleanWhitespace(node);
          if ((node.attributes && node.attributes.length > 0)
           || (node.hasChildNodes() && node.childNodes[0].nodeType ==
1)) {
            var localHash = {};
            if (node.attributes && node.attributes.length >= 1) {
              $A(node.attributes).each(function (attr) {
localHash[attr.nodeName] = [attr.nodeValue]; });
            }
      if (node.hasChildNodes() && node.childNodes[0].nodeType == 1) {
            $A(node.childNodes).each(function (node) {
this._subNodeToHash(localHash, node); }.bindAsEventListener(this));
      }
      else if (node.hasChildNodes()) {
        localHash['text'] = [this._nodeAsText(node)];
      }
      $H(localHash).each( function (pair) { if (pair[1].length == 1 &&
typeof pair[1][0] == 'string') { localHash[pair[0]] = pair[1][0]; } });
            return localHash;
          }
          else {
            return this._nodeAsText(node);
          }
        },
  _subNodeToHash: function (hash, node) {
          var key = node.tagName;
          if (hash[key]) {
            hash[key].push(this._nodeToHash(node));
          }
          else {
            hash[key] = [ this._nodeToHash(node) ];
          }
  },
  _nodeAsText: function (node) {
    return node.textContent || node.innerText || node.text || '';
  }
} );

I think this addresses the 'text' thing as well as keeping things in an
array if there are sub-elements.  So, now your example should be pretty
much spot-on (except lowercase text vs. TEXT).

Greg

> -----Original Message-----
> From: [EMAIL PROTECTED]
[mailto:rails-spinoffs-
> [EMAIL PROTECTED] On Behalf Of Ryan Gahl
> Sent: Friday, March 17, 2006 3:01 PM
> To: rails-spinoffs@lists.rubyonrails.org
> Subject: RE: [Rails-spinoffs] Updated the xml code to be more object-
> oriented
> 
> No problem, always try to play Devil's advocate...
> 
> Your tool is very nice, I can think of a few applications already.
> Thanks for the work.
> 
> The information transmitted in this electronic mail is intended only
for
> the
> person or entity to which it is addressed and may contain
confidential,
> proprietary, and/or privileged material.  Any review, retransmission,
> dissemination or other use of, or taking of any action in reliance
upon,
> this information by persons or entities other than the intended
recipient
> is prohibited. If you received this in error, please contact the
sender
> and
> delete the material from all computers.
> 
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to