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 🙏

© 2026 – Pkg Stats / Ryan Hefner

event-streaming-xml-parser

v2.0.0

Published

Utilize an event streaming parser to quickly and efficiently parse XML files

Downloads

76

Readme

Event Streaming XML Parser

This package utilizes an event streaming parser to quickly and efficiently process XML files. Some XML files tend to be quite large and many XML tools I found on npm try to load everything into memory which took too long.

Most of this was cobbled together from https://github.com/lddubeau/saxes/issues/32#issuecomment-770375996. I've simply wrapped it up in a package to reuse easier :gift:

Requirements

  • Node.js >= v22
  • ESM

Install

npm install event-streaming-xml-parser

Usage

import { parseXml } from 'event-streaming-xml-parser';

await parseXml({
  filename: 'example.xml',
  listeners: {
    opentag: (tag) => {
      // code here
    },
    text: (text) => {
      // code here
    },
    closetag: (tag) => {
      // code here
    },
    end: () => {
      // code here
    },
  },
});

API

This package exports the primary function parseXml, along with various helper functions for writing new streams createTagOpenXml, createTagCloseXml, escapeXmlElement, escapeXmlAttribute. There is no default export.

parseXml(options: ParseXmlFileOptions)

options

  • filename (string) -- XML file to parse
  • encoding (string, default: utf8) -- sets the character encoding for data read from the Readable stream
  • listeners (ParseXmlFileListeners) -- event listeners

Returns Promise<void>

Types

type ParseXmlFileOptions = {
  filename: string;
  encoding?: BufferEncoding;
  listeners: ParseXmlFileListeners;
};

type ParseXmlFileListeners = {
  opentag?: EmitterListenerOpenTag;
  text?: EmitterListenerText;
  closetag?: EmitterListenerCloseTag;
  end?: EmitterListenerEnd;
};

type EmitterListenerOpenTag = (tag: SaxesTagPlain) => void | Promise<void>;
type EmitterListenerText = (text: string) => void | Promise<void>;
type EmitterListenerCloseTag = (tag: SaxesTagPlain) => void | Promise<void>;
type EmitterListenerEnd = () => void | Promise<void>;

Examples

Count body tu elements

import { parseXml } from 'event-streaming-xml-parser';

async function countBodyTuElements(filepath) {
  let count = 0;
  let inBody = false;
  await parseXml({
    filename: filepath,
    listeners: {
      opentag: (tag) => {
        if (tag.name === 'body') inBody = true;
        if (tag.name === 'tu' && inBody) count++;
      },
      text: (text) => {},
      closetag: (tag) => {
        if (tag.name === 'body') inBody = false;
      },
      end: () => {},
    },
  });
  return count;
}

const count = await countBodyTuElements('temp/huge.tmx');
console.log(`Total <tu> elements in <body>: ${count}`);

Search and replace within attributes and save as a new file

This example will search for en-us in any xml:lang or srclang attribute, and replace it with en-US. It uses both escapeXmlElement and escapeXmlAttribute helper functions for writing to the new output stream.

import fs from 'node:fs';
import {
  parseXml,
  escapeXmlElement,
  escapeXmlAttribute,
} from 'event-streaming-xml-parser';

const outputEncoding = 'utf-8';
const outputStream = fs.createWriteStream('temp/output.xml', {
  flags: 'w',
  outputEncoding,
});
outputStream.write(`<?xml version="1.0" encoding="${outputEncoding}"?>`);

const searchRegExp = new RegExp(`^en-us$`, 'i'); // case insensitive
const attributeNames = ['xml:lang', 'srclang'];

await parseXml({
  filename: 'temp/input.xml',
  listeners: {
    opentag: (tag) => {
      let output = `<${tag.name}`;
      for (const [key, value] of Object.entries(tag.attributes)) {
        let newValue = value;
        if (attributeNames.includes(key)) {
          newValue = value.replace(searchRegExp, `en-US`);
        }
        output += ` ${key}="${escapeXmlAttribute(newValue)}"`;
      }
      if (!tag.isSelfClosing) {
        output += `>`;
      }
      outputStream.write(output);
    },
    text: (text) => {
      outputStream.write(escapeXmlElement(text));
    },
    closetag: (tag) => {
      outputStream.write(tag.isSelfClosing ? `/>` : `</${tag.name}>`);
    },
    end: () => {
      outputStream.close();
    },
  },
});