npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sax-parser

v2.0.3

Published

An xml parser written in JavaScript

Downloads

1,125

Readme

SAX Parser

npm version License

sax-parser is an xml parser written in javascript.

Install

npm install sax-parser

API

SAX Parser provides a SAX2 parser interface that can take a string, file. The parser can take characters from the document in chunks. To send chunks of the document to the parser use 'parseString(xml)'

SAX Parser

new xml.SaxParser()

  • Instantiate a new SaxParser
  • returns: a SaxParser object

new xml.SaxParser(callback)

  • Instantiate a new SaxParser
  • returns: a SaxParser object
  • Arguments
    • callback - a function that accepts the new sax parser as an argument

Parse

parser.parseString(string)

  • Parse an in memory string
  • Arguments
    • string - a string representing the document to parse

parser.pause()

pauses parsing of the document

parser.resume()

resumes parsing of the document

Callbacks

parser.onStartDocument(function() {})

Called at the start of a document

parse.onEndDocument(function() {})

Called at the end of the document parse

parser.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {})

Called on an open element tag

  • Arguments
    • elem - a string representing the element name
    • attrs - an array of arrays: [[key, value], [key, value]]
    • prefix - a string representing the namespace prefix of the element
    • uri - the namespace URI of the element
    • namespaces - an array of arrays: [[prefix, uri], [prefix, uri]]

parser.onEndElementNS(function(elem, prefix, uri) {})

Called at the close of an element

  • Arguments
    • elem - a string representing the element name
      • prefix - a string representing the namespace prefix of the element
      • uri - the namespace URI of the element

parser.onCharacters(function(chars) {})

Called when a set of content characters is encountered

  • Arguments
    • chars - a string of characters

parser.onCdata(function(cdata) {})

Called when a CDATA is encountered

  • Arguments
    • cdata - a string representing the CDATA

parser.onComment(function(msg) {})

Called when a comment is encountered

  • Arguments
    • msg - a string representing the comment

parser.onWarning(function(msg) {})

Called when a warning is encountered

  • Arguments
    • msg - a string representing the warning message

parser.onError(function(msg) {})

Called when an error is encountered

  • Arguments
    • msg - a string representing the error message

EXAMPLE USAGE

var xml = require("./lib/sax-parser");

var parser = new xml.SaxParser(function(cb) {
  cb.onStartDocument(function() {});
  cb.onEndDocument(function() {});
  cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {
    console.log(
      "=> Started: " +
        elem +
        " uri=" +
        uri +
        " (Attributes: " +
        JSON.stringify(attrs) +
        " )"
    );
  });
  cb.onEndElementNS(function(elem, prefix, uri) {
    console.log("<= End: " + elem + " uri=" + uri + "\n");
    parser.pause(); // pause the parser
    setTimeout(function() {
      parser.resume();
    }, 100); //resume the parser
  });
  cb.onCharacters(function(chars) {
    console.log("<CHARS>" + chars + "</CHARS>");
  });
  cb.onCdata(function(cdata) {
    console.log("<CDATA>" + cdata + "</CDATA>");
  });
  cb.onComment(function(msg) {
    console.log("<COMMENT>" + msg + "</COMMENT>");
  });
  cb.onWarning(function(msg) {
    console.log("<WARNING>" + msg + "</WARNING>");
  });
  cb.onError(function(msg) {
    console.log("<ERROR>" + JSON.stringify(msg) + "</ERROR>");
  });
});

//example read from chunks
parser.parseString("<html><body>");
parser.parseString("<!-- This is the start");
parser.parseString(" and the end of a comment -->");
parser.parseString("and lots");
parser.parseString("and lots of text&am");
parser.parseString("p;some more.");
parser.parseString("<![CD");
parser.parseString("ATA[ this is");
parser.parseString(" cdata ]]>");
parser.parseString("</body");
parser.parseString("></html>");

Author

  • Copyright 2009 - 2010 © Rob Righter (@robrighter)
  • Contributions from David Joham

License

This project is licensed under the MIT License - see the LICENSE file for details