Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML in a Web Worker

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?

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>

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

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

like image 577
user1127813 Avatar asked May 08 '12 07:05

user1127813


2 Answers

according to the spec

The DOM APIs (Node objects, Document objects, etc) are not available to workers in this version of this specification.

I guess thats why DOMParser is not availlable, but I don't really understand why that decision was made. (fetching and processing an XML document in a WebWorker does not seems unreasonnable)

but you can import other tools available: a "Cross Platform XML Parsing in JavaScript"

like image 87
Mathieu Avatar answered Oct 06 '22 04:10

Mathieu


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

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

like image 26
Tobias Nickel Avatar answered Oct 06 '22 04:10

Tobias Nickel