JavaScript SAX Based Parser
Written by on April 7th, 2008 in Ajax News.
Gregory Reimer fancies SAX, and wished that a SAX parser was given to us by the JavaScript host environment. You can’t blame him for not living DOM, but how about E4X? Or, StAX? Anyway, Gregory decided to build a SAX based parser in JavaScript itself, using simple search and replace:
After reading Search and Don’t Replace over at John Resig’s blog, it got me wondering if you could use that technique as the basis for a SAX parser in JavaScript. Of course there’s nothing stopping you from building a SAX parser from scratch in JavaScript, but (methinks) the string tokenizer part of it would be a bit of a beast. However, by taking advantage of the optimization built into JavaScript’s RegExp replacement engine, you might just be able to work a nice little souped-up tokenizing engine out of the deal.
So I thought I’d give it a try. What I came up with is nowhere near anything resembling a real-world, valid XML parser. All it knows how to deal with are elements, text nodes and character entities. And not all of the error messages are as helpful as a real world implementation should be. And I’m sure there are plenty of bugs since I banged this out in less than an afternoon. But it ran like scalded cats on a 422kb file
You get to use the simple SAX modules a la:
-
-
function doStartTag(name){alert(”opening tag: “+name);}
-
function doEndTag(name){alert(”closing tag: “+name);}
-
function doAttribute(name,val){alert(”attribute: “+name+’="’+val+’"’);}
-
function doText(str){
-
str=str.normalize();
-
if(!str){str=’[whitespace]‘;}
-
alert(”encountered text node: “+str);
-
}
-
Downlaod the SAX parser.
Source: Ajaxian
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/265540491/javascript-sax-based-parser