sax
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Sax is an evented streaming XML parser written in JavaScript. It's designed with Node.js in mind but is also suitable for use in the browser or other CommonJS implementations. It provides a very simple tool for parsing through an XML string and can also serve as a stepping stone towards creating a streaming HTML parser. While it is not an HTML parser, DOM builder, or XML validator, it can prove handy when you are dealing with RSS and other mostly-ok-but-kind-of-broken XML documents.
To use it, you first need to require the Sax module. You can then create a new instance of the Sax parser with the strict mode set to either true or false; where setting it to false will enable html-mode. The parser lets you listen for various events such as errors, text, opentags, attributes, and end of parsing. Upon encountering these events, it will call the assigned event listener functions.
Here's a code snippet of how to use the Sax parser in JavaScript:
var sax = require("./lib/sax"),
strict = true,
parser = sax.parser(strict);
parser.onerror = function (e) {
// an error happened.
};
parser.ontext = function (t) {
// got some text. t is the string of text.
};
parser.onopentag = function (node) {
// opened a tag. node has "name" and "attributes"
};
parser.onattribute = function (attr) {
// an attribute. attr has "name" and "value"
};
parser.onend = function () {
// parser stream is done, and ready to have more stuff written to it.
};
parser.write('<xml>Hello, <who name="world">world</who>!</xml>').close();
The Sax package also supports stream usage and includes a variety of methods (like write, close, resume) and members (like line, column, position, startTagPosition, closed, strict, etc) which can be used as per the requirement.
The Sax package documentation can be found in the readme of the GitHub repository. The documentation includes detailed descriptions of the Sax parser's intended use, capabilities along with examples of how to use the module. The readme also comprehensively lists the arguments, methods, members, and events supported by the Sax parser.