Hi Gregory

What you think about this

/* -------------------------------------- */

XMLDoc = Class.create();

Object.extend(XMLDoc.prototype,
{
    initialize: function (XMLFile, options)
    {
        this.xmlFile = XMLFile;
        this.options = options;
        this.element = false;

        this.getXml();
    },

    XMLHash: function ()
    {
        return this.XMLParsed;
    },

    asHash: function ()
    {
        if (! this._xmlHash) {
            this._xmlHash = this._nodeToHash(this.element);
        }
        return this._xmlHash;
    },

    getXml: function ()
    {
        if(this.options.onLoading) {
            this.options.onLoading();
        }
        new Ajax.Request(this.xmlFile, {method:'get', onComplete:
this.parseXML.bind(this)});
    },

    parseXML: function (req)
    {
        this.element = req.responseXML;
        var XMLParsed = this.asHash();

        if(this.options.onComplete) {
            this.options.onComplete(XMLParsed);
        }
    },

    _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 || '';
    }
}
);


/* -------------------------------------- */


Usage: 

/* ************** */

function showRssLength(xml)
{
    /* this is an example */
    alert(xml.rss[0]['channel'][0]['item'].length);
}

new XMLDoc('rssFile.xml',{onComplete:showRssLength});

/* ************** */







On Fri, 2006-03-17 at 22:35, Gregory Hill wrote:
> 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
-- 
/**
 * Claudio Gamboa
 * @email: [EMAIL PROTECTED]
 * @IM-Sapo: [EMAIL PROTECTED]
 */

_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

Reply via email to