Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

dom - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...