Activate your free membership today | Log-in

Monday, April 7th, 2008

JavaScript SAX Based Parser

Category: JavaScript, Library

<p>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.

Related Content:

  • Xerces
    Xerces (the name comes from the Xerces blue butterfly) is a set of parsers compatible with Extensible Markup Language...
  • Sax Parsers
  • Xalan
    Xalan is a specification for transforming Extensible Markup Language (XML) documents into Hypertext Markup Language (HTML) or other XML document...
  • Using XML pipelines - part 1
    In this Web services advisor, William Brogden discusses the SAX toolkit for pipeline processing of...
  • SAX (Simple API for XML)
    SAX (Simple API for XML) is an application program interface (API) that allows a programmer to interpret a Web file that uses the Extensible Markup...

Posted by Dion Almaer at 4:00 am
2 Comments

-----
-27272724.5 rating from 11 votes

2 Comments »

Comments feed TrackBack URI

A JS SAX parser has been available for a while from Mozilla’s JSLib:

http://jslib.mozdev.org/libraries/utils/sax.js.html

However, I personally only see SAX as a relativiely minor step up from straight DOM code, I prefer something a little more high-level, such a JS implementation of Apache Commons Digester:

http://books.google.com/books?id=tkARdW-sRoAC&pg=PA246&lpg=PA246&dq=jsdigester&source=web&ots=Yr1OCWMzCE&sig=MIR_i3TNQHLYDY-T0-5y_9-gkdw&hl=en#PPA231,M1

Or, for the Java enthusiasts:

http://javawebparts.sourceforge.net/javadocs/javawebparts/taglib/jstags/JSDigesterTag.html

I wonder if Gregory’s implementation might make JSDigester more performant? Might be an interesting experiment, if I can find the time.

Comment by fzammetti — April 7, 2008

SAX is a somewhat outdated xml parsing model, the latest and most adavnced model is vtd-xml

http://vtd-xml.sf.net

Comment by jzhang2009 — December 2, 2009

Leave a comment

You must be logged in to post a comment.