# node-expat

> NodeJS binding for fast XML parsing.

Latest version **2.4.1** (published 2024-03-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-expat
pnpm add node-expat
yarn add node-expat
bun add node-expat
```

## Health

**Score 33/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.4.1 |
| Published | 2024-03-08 |
| First published | 2011-03-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/node-expat) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 4.2 MB |
| Known vulnerabilities | 0 |
| Install scripts | yes |
| GitHub stars | 384 |
| Author | Astro |
| Maintainers | chris-rock, y--, astro, lloydwatkin, tkel |
| Keywords | xml, sax, expat, libexpat, parse, parsing |

## Links

- npm: https://www.npmjs.com/package/node-expat
- Repository: https://github.com/astro/node-expat
- Homepage: http://github.com/astro/node-expat
- Issues: https://github.com/astro/node-expat/issues
- npm.io page: https://npm.io/package/node-expat

## Dependencies (2)

- [nan](https://npm.io/package/nan.md) ^2.19.0
- [bindings](https://npm.io/package/bindings.md) ^1.5.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 2.4.1 (latest) — 2024-03-08
- 2.4.0 — 2021-03-12
- 2.3.18 — 2019-05-29
- 2.3.17 — 2018-08-10
- 2.3.16 — 2017-07-05
- 2.3.15 — 2016-06-30
- 2.3.14 — 2016-06-27
- 2.3.13 — 2016-01-31
- 2.3.12 — 2015-12-09
- 2.3.11 — 2015-10-26
- 2.3.10 — 2015-09-09
- 2.3.9 — 2015-08-28
- 2.3.8 — 2015-05-13
- 2.3.7 — 2015-03-06
- 2.3.6 — 2015-02-16
- … 33 more at https://npm.io/package/node-expat/versions

## README

# node-expat

[![build status](https://img.shields.io/travis/astro/node-expat/master.svg?style=flat-square)](https://travis-ci.org/astro/node-expat/branches)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)

## Motivation

You use [Node.js](https://nodejs.org) for speed? You process XML streams? Then you want the fastest XML parser: [libexpat](http://expat.sourceforge.net/)!

## Install

```
npm install node-expat
```

## Usage

Important events emitted by a parser:

```javascript
(function () {
  "use strict";

  var expat = require('node-expat')
  var parser = new expat.Parser('UTF-8')

  parser.on('startElement', function (name, attrs) {
    console.log(name, attrs)
  })

  parser.on('endElement', function (name) {
    console.log(name)
  })

  parser.on('text', function (text) {
    console.log(text)
  })

  parser.on('error', function (error) {
    console.error(error)
  })

  parser.write('<html><head><title>Hello World</title></head><body><p>Foobar</p></body></html>')

}())

```

## API

* `#on('startElement' function (name, attrs) {})`
* `#on('endElement' function (name) {})`
* `#on('text' function (text) {})`
* `#on('processingInstruction', function (target, data) {})`
* `#on('comment', function (s) {})`
* `#on('xmlDecl', function (version, encoding, standalone) {})`
* `#on('startCdata', function () {})`
* `#on('endCdata', function () {})`
* `#on('entityDecl', function (entityName, isParameterEntity, value, base, systemId, publicId, notationName) {})`
* `#on('error', function (e) {})`
* `#stop()` pauses
* `#resume()` resumes

## Error handling

We don't emit an error event because libexpat doesn't use a callback
either. Instead, check that `parse()` returns `true`. A descriptive
string can be obtained via `getError()` to provide user feedback.

Alternatively, use the Parser like a node Stream. `write()` will emit
error events.

## Namespace handling

A word about special parsing of *xmlns:* this is not necessary in a
bare SAX parser like this, given that the DOM replacement you are
using (if any) is not relevant to the parser.

## Benchmark

`npm run benchmark`

| module                                                                                | ops/sec | native | XML compliant | stream         |
|---------------------------------------------------------------------------------------|--------:|:------:|:-------------:|:--------------:|
| [sax-js](https://github.com/isaacs/sax-js)                                            |  99,412 | ☐      | ☑             | ☑              |
| [node-xml](https://github.com/dylang/node-xml)                                        | 130,631 | ☐      | ☑             | ☑              |
| [libxmljs](https://github.com/polotek/libxmljs)                                       | 276,136 | ☑      | ☑             | ☐              |
| **node-expat**                                                                        | 322,769 | ☑      | ☑             | ☑              |

Higher is better.

## Testing

```
npm install -g standard
npm test
```

## Windows

If you fail to install node-expat as a dependency of node-xmpp, please update node-xmpp as it doesn't use node-expat anymore.

Dependencies for `node-gyp` https://github.com/TooTallNate/node-gyp#installation

See https://github.com/astro/node-expat/issues/78 if you are getting errors about not finding `nan.h`.

### expat.vcproj

```
VCBUILD : error : project file 'node-expat\build\deps\libexpat\expat.vcproj' was not found or not a valid proj
ect file. [C:\Users\admin\AppData\Roaming\npm\node_modules\node-expat\build\bin
ding.sln]
```

Install [Visual Studio C++ 2012](http://go.microsoft.com/?linkid=9816758) and run npm with the [`--msvs_version=2012` flag](http://stackoverflow.com/a/16854333/937891).

---
_Source: https://npm.io/package/node-expat · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
