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

v0.3.0

Published

Low-memory streaming xml parser for node.js. Returns each node as an object. Uses node-expat

Downloads

18

Readme

xml-object-stream

Streaming parsers are hard to work with, but sometimes you need to parse a really big file. This module gives you the best of both worlds. You give it a specific node to look for, and it will return each of those nodes as an object, one at a time, without loading the whole document into memory at once.

Uses node-expat for fast(est) xml processing.

Installation

npm install xml-object-stream

Parsing a ReadStream

Let's say we have a file, hugePersonDirectory.xml, that looks something like this:

<root>
  <people>
    <person>...</person>
    <person>...</person>
    <person>...</person>
    <person>...</person>
    <person>...</person>
  </people>
<root>

You want to do something with each person object, but you can't load them all into memory at once.

xml = require 'xml-object.stream'
fs = require 'fs'

readStream = fs.createReadStream 'hugePersonDirectory.xml'
parser = xml.parse readStream

parser.each 'person', (person) ->
  # do something with the person!

The parser emits some streaming events

parser.on 'end', ->
parser.on 'error', (err) ->
parser.on 'close', ->

You can pause and resume it if the xml parser gets too far ahead of your processing

parser.pause()

# then when you catch back up
parser.resume()

Since the parser takes any read stream, you can use it to parse urls without saving them to disk.

Node Object Format

Nodes are converted to objects. For the following xml:

<person id="asdf123">
  <firstName>Bob</firstName>
  <lastName>Wilson</lastName>
  <employee id="asdf123"/>
  <note author="Joe">Bob is a poor worker</note>
  <note author="Jim">Bob spends all his time parsing xml</note>
</person>

You can access attributes with the $ property

person.$.id == "asdf123"

You can access the last child of a given name by its name. Text is accessed with $text

person.firstName.$text == "Bob"

Node names are available under the $name property

person.$name == "person"

Every child node is put into the $children array

notes = person.$children.filter (child) ->
  return (child.$name is "note")

notes[0].$.author == "Joe"

API Reference

exports.parse = (nodeReadStream, [options]) ->
  # returns a Parser

class Parser

  # calls cb each time it finds a node with that name
  each: (nodeName, cb) ->

  # bind to 'end', 'error', and 'close'
  on: (eventName, cb) ->

  # pause or resume the read stream to let you processor catch up
  pause: ->
  resume: ->
  

Options

{
  # removes all namespace information from the node names
  stripNamespaces: true
}