解析Web Worker中的XML [英] Parsing XML in a Web Worker

查看:201
本文介绍了解析Web Worker中的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用DOMParser对象将文本字符串解析为XML树。但是,它在Web Worker的上下文中不可用(当然,也不是document.ELEMENT_NODE或者需要的各种其他常量)。有没有其他方法可以做到这一点?

I have been using a DOMParser object to parse a text string to an XML tree. However it is not available in the context of a Web Worker (and neither is, of course, document.ELEMENT_NODE or the various other constants that would be needed). Is there any other way to do that?

请注意,我不想操纵当前页面的DOM。 XML文件不包含HTML元素或任何类型的元素。实际上,我根本不想触摸文档对象。我只是想提供如下文本字符串:

Please note that I do not want to manipulate the DOM of the current page. The XML file won't contain HTML elements or anything of the sort. In fact, I do not want to touch the document object at all. I simply want to provide a text string like the following:

<car color="blue"><driver/></car>

...并找回合适的树形结构和遍历它的方法。我也不关心模式验证或任何花哨的东西。我知道 XML for< SCRIPT> ,许多人可能觉得有用(因此我在这里链接到它),但它的许可并不适合我。我不确定jQuery是否包含XML解析器(我对这个东西很新),但即使它确实(并且它可以在Worker中使用),我也不会包含额外的~50K代码行这个函数。

...and get back a suitable tree structure and a way to traverse it. I also do not care about schema validation or anything fancy. I know about XML for <SCRIPT>, which many may find useful (hence I'm linking to it here), however its licensing is not really suitable for me. I'm not sure if jQuery includes an XML parser (I'm fairly new to this stuff), but even if it does (and it is usable inside a Worker), I would not include an extra ~50K lines of code just for this function.

我想我可以在JavaScript中编写一个简单的XML解析器,我只是想知道我是否错过了更快的选项。

I suppose I could write a simple XML parser in JavaScript, I'm just wondering if I'm missing a quicker option.

推荐答案

此时我想分享我的解析器: https://github.com/tobiasnickel/tXml

At this point I like to share my parser: https://github.com/tobiasnickel/tXml

使用tXml()方法可以将字符串解析为对象,只需要0.5kb缩小+ gzipped

with its tXml() method you can parse a string into an object and it takes only 0.5kb minified + gzipped

这篇关于解析Web Worker中的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆