Sam Collett wrote:
我历尽千辛万苦,但是也没有找到使用jQuery能够序列化XML的方法。是不是我没有找到,或者需要我从头开始写一个新的插件($.fn.serializeXML)
我知道Firefox有XMLSerializer()这么一个方法,但是对于IE、Safari、Opera呢?
我想的一种能够支持多种序列化是比较好的:
JS Object <--> XML <--> JSON <--> JS Object
实质上,jQuery提供了一个插件,能够实现XML的序列化问题:
http://dev.jquery.com/wiki/Plugins/toXML
Trackback: http://osdir.com/ml/lang.javascript.jquery/2006-10/msg00079.html
原文:
Sam Collett wrote:
> On 02/10/06, Mark Gibson <mgibson@xxxxxxxxxxxxxxx> wrote:
>> Hello,
>> I've search high and low, but can't find a method of serializing XML
>> with jQuery. Have I missed something, or should I start writing a
>> new plugin? ($.fn.serializeXML)
>>
>> I know that firefox has XMLSerializer(), any ideas for IE, Safari,
>> Opera? Maybe just a hand coded JS serializing routine?
>
> I don't think there is any way of serializing XML in jQuery without
> resorting to a plugin (I don't know of any plugins that can do this).
>
> I think a multi-purpose serializer would be good:
> JS Object <--> XML <--> JSON <--> JS Object
I was specifically thinking of just serializing DOM objects to strings,
anything beyond that requires some kind of mapping.
Here's a simple implementation using the XMLSerialize object:
- $.fn.serializeXML = function () {
- var out = '';
- if (typeof XMLSerializer == 'function') {
- var xs = new XMLSerializer();
- this.each(function() {
- out += xs.serializeToString(this);
- });
- } else {
- // TODO: Find another serializer, or manually serialize
- }
- return out;
- };
This will need to be fleshed out for other browsers.
Does anyone know of native serialization methods in IE, Safari, Opera?
or do any of these support XMLSerialize()?
- Mark Gibson
Subject: Re: [jQuery] New plugin: toXML (XML serializer)
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