博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[jQuery] New plugin: toXML (XML serializer)
阅读量:4128 次
发布时间:2019-05-25

本文共 3537 字,大约阅读时间需要 11 分钟。

Sam Collett wrote:

我历尽千辛万苦,但是也没有找到使用jQuery能够序列化XML的方法。是不是我没有找到,或者需要我从头开始写一个新的插件($.fn.serializeXML)

我知道Firefox有XMLSerializer()这么一个方法,但是对于IE、Safari、Opera呢?

我想的一种能够支持多种序列化是比较好的:

JS Object <--> XML <--> JSON <--> JS Object

实质上,jQuery提供了一个插件,能够实现XML的序列化问题:

 

Trackback:

 

 

原文:

Sam Collett wrote:

> On 02/10/06, Mark Gibson <> 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:

>
>

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

转载地址:http://mqzvi.baihongyu.com/

你可能感兴趣的文章
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>
pytorch(三)
查看>>
ubuntu相关
查看>>
C++ 调用json
查看>>
nano中设置脚本开机自启动
查看>>
动态库调动态库
查看>>
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>