Hi,

> I've submitted the XML serializer plugin to the wiki:
>
> http://jquery.com/docs/Plugins/toXML/

Since Trac doesn't have a possibility to discuss such things before editing 
the Webpage I'd like to post two other Variants here for discussion:

Simulate XMLSerializer globally and use it always - change the serialization 
function on first call to distinguish between IE and others.
----
if (typeof XMLSerializer == 'function') {
        XMLSerializer = function() {};
        XMLSerializer.prototype.serializeToString = function(node) {
                var fn = function(node) {return node.xml; };
                if (this[0].xml !== undefined) {
                        fn = function(node) {
                            // TODO: Manually serialize DOM here, for browsers
                            // that support neither of two methods above.
                        };
                }
                XMLSerializer.prototype.serializeToString = 
this.serializeToString = fn
                return fn(node);
        }
}

$.fn.toXML = function () {
        var out = '';
        if (this.length > 0) {
                var xs = new XMLSerializer();
                this.each(function() {
                        out += xs.serializeToString(this);
                });
        }
        return out;
};
----

Change the toXML-function at first call
----
$.fn.toXML = function () {
        var fn = function() {
                var out = '';
                this.each(function() { out += this.xml; });
                return out;
        };
        if (typeof XMLSerializer == 'function') {
                fn = function() {
                        var out = '';
                        var xs = new XMLSerializer();
                        this.each(function() {
                                out += xs.serializeToString(this);
                        });
                        return out;
                };
        } else if (this[0].xml === undefined) {
                fn = function() {
                        var out = '';
                        // TODO: Manually serialize DOM here, for browsers
                        // that support neither of two methods above.
                        return out;
                };
        }

        $.fn.toXML = fn;
        return fn.apply(this,[]);
};
----

I admit, that the two suggestions might not be to easy to understand. I like 
to play with functions as values :-)

At least the first one provides a more general solution by simulating 
XMLSerializer and both of them are faster after the first call. That might be 
a criterium if you work with large Datasets. If performance is imortant to 
you, you might also consider to replace the calls to each() with for-loops.

Christof

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to