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-stream-parser

v1.0.1

Published

A high-performance, streaming XML parser that converts XML to JSON while preserving attributes, namespaces, and structure

Downloads

177

Readme

XML Stream Parser

A high-performance, streaming XML parser for Node.js that converts XML to JSON while preserving attributes, namespaces, and structure. Built with TypeScript for type safety and better developer experience.

Features

  • 🚀 Streaming API for efficient memory usage with large XML files
  • 🔄 Preserves XML attributes and text content
  • 🌳 Handles deeply nested XML structures
  • 📚 Full namespace support
  • 📦 Automatic array handling for repeated elements
  • 💪 TypeScript support with type definitions
  • 🧪 Comprehensive test suite

Installation

npm install xml-stream-parser
# or
yarn add xml-stream-parser

Usage

Basic Example

import { XMLStreamParser } from 'xml-stream-parser';
import { createReadStream } from 'fs';

const parser = new XMLStreamParser();

// Handle parsed data
parser.on('data', (data) => {
  console.log(JSON.stringify(data, null, 2));
});

// Handle errors
parser.on('error', (error) => {
  console.error('Parsing error:', error);
});

// Parse XML from a file
createReadStream('example.xml').pipe(parser);

Example with Attributes and Text Content

const xml = `
<root>
  <amount currency="USD">500.00</amount>
  <details type="shipping">
    <address country="US">
      <street>123 Main St</street>
      <city>New York</city>
    </address>
  </details>
</root>
`;

// Will output:
{
  "root": {
    "amount": {
      "#text": "500.00",
      "currency": "USD"
    },
    "details": {
      "type": "shipping",
      "address": {
        "country": "US",
        "street": "123 Main St",
        "city": "New York"
      }
    }
  }
}

Handling Repeated Elements

const xml = `
<root>
  <items>
    <item>1</item>
    <item>2</item>
    <item>3</item>
  </items>
</root>
`;

// Will output:
{
  "root": {
    "items": {
      "item": ["1", "2", "3"]
    }
  }
}

API Reference

XMLStreamParser

The main class that extends Node.js Transform stream.

Constructor

constructor();

Creates a new instance of XMLStreamParser with default settings.

Events

  • 'data': Emitted when the XML document has been parsed into a JSON object
  • 'error': Emitted when an error occurs during parsing
  • 'end': Emitted when the parser has completed processing all input

Types

interface XMLNode {
  [key: string]: XMLValue | undefined;
}

type XMLValue = string | XMLNode | XMLValue[];

Features in Detail

Attribute Handling

Attributes are preserved as properties on the node object. When a node has both attributes and text content, the text is stored in the #text property.

Namespace Support

XML namespaces are preserved in the output JSON structure, maintaining the original namespace prefixes.

Array Handling

Elements that appear multiple times at the same level are automatically converted to arrays.

Testing

The package includes a comprehensive test suite. To run the tests:

yarn test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

Development

  1. Clone the repository
  2. Install dependencies: yarn install
  3. Build the project: yarn build
  4. Run tests: yarn test
  5. Development mode: yarn dev

Type Checking

Run type checking:

yarn typecheck