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

xml-object-stream-sax

v0.5.0

Published

Wrapper for sax.js that extracts objects that match an xpath-ish expression from an xml stream.

Downloads

14

Readme

xml-object-stream-sax

xml-object-stream-sax is a simple wrapper on top of sax.js that extracts json objects that match a given xpath-ish expression from an xml stream and passes them to a callback or stuffs them into a promised array.

Usage

var xos = require('xml-object-stream-sax')(/* config object */);
var xml = '<library>' +
          '<book published="1622"><author>John</author></book>' +
          '<book author="Jane"><author country="Uganda">Jane</author></book>' +
          '</library>';
xos(xml, '//book', function(book) { console.log(book); });

Configuration

The config object is passed straight through to sax.js createStream, so any additional options applicable there are also applicable here.

strict

default: true

Passed through to sax.js createStream to turn on strict parsing. Non-strict parsing assumes icase is true.

pojo

default: false

Instead of creating a dom-ish tree, plain object trees will be assembled with xml attributes and children set as properties. Text is available as the text property. Same-named attributes and children are turned into array properties. If there are no children or attributes, the text will be used instead of an object.

icase

default: true

If set to true, the xpath-ish query will be case-insensitive. Non-strict mode enables this by default.

chunk

default: 8196

If the callback function accepts a done callback, then xml will be processed chunk characters at a time. So, there will be a short flood of match callbacks and the xml processor will wait until all of the callbacks are complete before releasing the next chunk. This is convenient for processing very large xml files with callbacks that do a lot of I/O. Instead of firing and queuing tons of callbacks while the rest of xml processing uses the cpu, the xml processor yields control until all of the callbacks are done for each chunk.

Arguments

function(xml, xpath[, callback]);

xml

This can be a string containing xml, in which case, it will be written directly to the sax stream.

It can be a string starting with 'http://', which will be opened with the node http module as a stream and piped to the sax stream.

It can be a string starting with 'file://', which will have the first 7 characters stripped off, be opened with the node fs module as a stream, and piped to the sax stream.

It can be an object with a pipe method, which will be called with the sax stream.

Anything else will cause an exception to be thrown.

xpath

This is a very limited subset of xpath-ish syntax. /foo/bar will match bar nodes attached to a foo node, which is the root element. //bar will match any bar node in the hierarchy, including nested bar nodes. The direct descendent / and descendent // selectors may be intermingled as needed. /foo//bar//baz/bat will match bat nodes directly descended from baz nodes, which have bar ancestors, which have a foo root ancestor.

The matching is done using a non-gready regular expression made out of the xpath string. Only nodes that match or descend from a match of the xpath are kept around while the stream is being processed, so (theoretically) very large chunks of xml that have bits that are interesting deeply embedded in them should not consume too many resources.

callback

function(node[, callback]);

For each matching object, the callback will be called with the matched object. If the callback itself accepts a callback, it is expected to call the callback when it is done so that processing may resume.

If no callback is provided a promise will be returned. If the promise is resolved, it will contain an array of all matched objects.

If a callback is provided, an object with an onEnd method will be returned. The onEnd method accepts a callback that will be called when all processing is complete.

Changes

0.5.0

  • CDATA is now considered to be text.
  • Typings are now provided.

0.4.0

  • Text from unmatched nodes is now discarded thanks to @sheershoff. Matching should also have slightly less overhead.

0.3.0

  • BUG: Fixed issues with the wrapped readable stream.
  • The global Promise is now used rather than when.

0.2.0

  • If the parser detects a URL starting with https:// as xml input, it will use the proper client to open the stream.

0.1.0

  • Harmony is no longer required.
  • Multiple queries can be run simultaneously by passing an array of XPath-ish strings.
  • If a callback is supplied, the object returned from the initial call to process xml will have an onEnd method that can be used to set a callback to be triggered after processing is complete.