Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML in Web Workers

I know the Web Worker spec says "no access to DOM because DOM is not thread safe". While I can see that's logical for web workers and the HTML page DOM, it's actually very restrictive when considering parsing XML from an XmlHttpRequest call - after all, the basic handling of that call is asynchronous and so has little effect on the foreground thread, it's the parsing of the XML that slows down the foreground thread (when dealing with XML apps).

Is there any way other than building my own XML parser in Javascript (I'm not going the XPCOM route!) to parse XML in the Web Worker?

like image 513
Martin Bartlett Avatar asked Feb 03 '12 18:02

Martin Bartlett


People also ask

How does parsing XML work?

XML parser is a software library or a package that provides interface for client applications to work with XML documents. It checks for proper format of the XML document and may also validate the XML documents. Modern day browsers have built-in XML parsers. The goal of a parser is to transform XML into a readable code.

How is XML parsing done with sax in web technology?

The application receives document information from the parser through a ContentHandler object. It implements various event handlers in the interface methods in ContentHandler, and registers the ContentHandler object with the SAX parser. The parser reads an XML document from the beginning to the end.

Can browser parse XML?

All major browsers have a built-in XML parser to access and manipulate XML.


1 Answers

Um - answering myself a few hours later - but this may well be a piece of information that proves very useful to people:

There IS an open-source, full function XML parser written entirely in javascript - and it works very well in Web Workers:

XML-JS

Basically, you just need to include tinyxmlsax.js and tinyxmlw3cdom.js in your worker, then follow the doc for using the W3C DOM.

With very little massaging, I got my code to work with both the normal DOMParser (for when running in the foreground) and the xmljs parser.

Obviously its slower - but that really isn't too much of an issue - after all, you're running it in the background!

like image 141
Martin Bartlett Avatar answered Oct 19 '22 11:10

Martin Bartlett