BizTalk Utilities CV ,   Jobs ,   Code library
 
Go to the front page to continue learning about XML or select below:

Contents

ReBlogger Contents

Previous posts in XML

 
 
Page 4083 of 19626

JavaScript SAX Based Parser

Blogger : Ajaxian Blog
All posts : All posts by Ajaxian Blog
Category : XML
Blogged date : 2008 Apr 07

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:

JAVASCRIPT:
  1.  
  2. function doStartTag(name){alert("opening tag: "+name);}
  3. function doEndTag(name){alert("closing tag: "+name);}
  4. function doAttribute(name,val){alert("attribute: "+name+'="'+val+'"');}
  5. function doText(str){
  6.     str=str.normalize();
  7.     if(!str){str='[whitespace]';}
  8.     alert("encountered text node: "+str);
  9. }
  10.  

Downlaod the SAX parser.


Read comments or post a reply to : JavaScript SAX Based Parser
Page 4083 of 19626

Newest posts
 

    Email TopXML