Take the 2-minute tour ×

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.

share|improve this question
add comment

1 Answer

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"

share|improve this answer
Apparently some browsers like Firefox have to use the main thread in the XML parser, source: w3-org.9356.n7.nabble.com/… –  Joren Van Severen Aug 7 '13 at 11:47
add comment

Your Answer


