Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sax

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sax

An evented streaming XML parser in JavaScript

  • 1.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
38M
increased by3.29%
Maintainers
1
Weekly downloads
 
Created

What is sax?

The sax npm package is a streaming XML parser that is designed for speed and simplicity. It follows the SAX parsing approach, which is an event-based model for parsing XML documents. This allows developers to handle different parts of the document as they are parsed without keeping the entire document in memory.

What are sax's main functionalities?

Parsing XML

This code demonstrates how to parse an XML string. It creates a new SAX parser, sets up an event listener for the 'opentag' event to log the name and attributes of each tag, and then writes an XML string to the parser.

const sax = require('sax'),
  parser = sax.parser(true);
parser.onopentag = function (node) {
  // node has attributes with string values
  console.log(node.name + ' - ' + JSON.stringify(node.attributes));
};
parser.write('<xml><tag attr="value">content</tag></xml>').close();

Stream Parsing

This code demonstrates how to parse XML from a file stream. It creates a SAX stream, sets up an event listener for the 'opentag' event, and then pipes a read stream from a file into the SAX stream.

const sax = require('sax'),
  fs = require('fs'),
  saxStream = sax.createStream(true);
saxStream.on('opentag', function (node) {
  console.log(node.name + ' - ' + JSON.stringify(node.attributes));
});
fs.createReadStream('file.xml').pipe(saxStream);

Error Handling

This code demonstrates how to handle errors during parsing. It sets up an error event listener on the SAX parser to handle any parsing errors.

const sax = require('sax'),
  parser = sax.parser(true);
parser.onerror = function (e) {
  // an error happened.
};
parser.write('<xml>this is some malformed xml</xml>').close();

Other packages similar to sax

FAQs

Package last updated on 27 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc