[jQuery] New plugin: toXML (XML serializer)

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:

  1. $.fn.serializeXML = function () {
  2. var out = '';
  3. if (typeof XMLSerializer == 'function') {
  4. var xs = new XMLSerializer();
  5. this.each(function() {
  6. out += xs.serializeToString(this);
  7. });
  8. else {
  9. // TODO: Find another serializer, or manually serialize
  10. }
  11. return out;
  12. };

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.
---

  1. if (typeof XMLSerializer == 'function') {
  2. XMLSerializer = function() {};
  3. XMLSerializer.prototype.serializeToString = function(node) {
  4. var fn = function(node) {return node.xml; };
  5. if (this[0].xml !== undefined) {
  6. fn = function(node) {
  7. // TODO: Manually serialize DOM here, for browsers
  8. // that support neither of two methods above.
  9. };
  10. }
  11. XMLSerializer.prototype.serializeToString = 
  12. this.serializeToString = fn
  13. return fn(node);
  14. }
  15. }
  16. $.fn.toXML = function () {
  17. var out = '';
  18. if (this.length > 0) {
  19. var xs = new XMLSerializer();
  20. this.each(function() {
  21. out += xs.serializeToString(this);
  22. });
  23. }
  24. return out;
  25. };
  26. ----
  27. Change the toXML-function at first call
  28. ----
  29. $.fn.toXML = function () {
  30. var fn = function() {
  31. var out = '';
  32. this.each(function() { out += this.xml; });
  33. return out;
  34. };
  35. if (typeof XMLSerializer == 'function') {
  36. fn = function() {
  37. var out = '';
  38. var xs = new XMLSerializer();
  39. this.each(function() {
  40. out += xs.serializeToString(this);
  41. });
  42. return out;
  43. };
  44. else if (this[0].xml === undefined) {
  45. fn = function() {
  46. var out = '';
  47. // TODO: Manually serialize DOM here, for browsers
  48. // that support neither of two methods above.
  49. return out;
  50. };
  51. }
  52. $.fn.toXML = fn;
  53. return fn.apply(this,[]);
  54. };

----

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值