# body-parser-xml

> XML parser middleware for express.js.

Latest version **2.0.5** (published 2023-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install body-parser-xml
pnpm add body-parser-xml
yarn add body-parser-xml
bun add body-parser-xml
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2023-04-11 |
| First published | 2015-12-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/body-parser-xml) |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 16 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 38 |
| Author | Tom Spencer |
| Maintainers | fiznool |
| Keywords | express, xml, middleware, body-parser |

## Links

- npm: https://www.npmjs.com/package/body-parser-xml
- Repository: https://github.com/fiznool/body-parser-xml
- Homepage: https://github.com/fiznool/body-parser-xml#readme
- Issues: https://github.com/fiznool/body-parser-xml/issues
- npm.io page: https://npm.io/package/body-parser-xml

## Dependencies (1)

- [xml2js](https://npm.io/package/xml2js.md) ^0.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.0.5 (latest) — 2023-04-11
- 2.0.4 — 2023-04-11
- 2.0.3 — 2021-05-19
- 2.0.2 — 2021-05-11
- 2.0.1 — 2020-12-02
- 2.0.0 — 2020-03-25
- 1.1.0 — 2016-03-18
- 1.0.0 — 2015-12-19

## README

# XML Body Parser

Adds XML parsing to the [body-parser](https://github.com/expressjs/body-parser) library, so you can convert incoming XML data into a JSON representation.

This is really useful if you want to deal with plain old JavaScript objects, but you need to interface with XML APIs.

[![Build Status](https://github.com/fiznool/body-parser-xml/workflows/Node.js%20CI/badge.svg)](https://github.com/fiznool/body-parser-xml/workflows/Node.js%20CI/badge.svg)
[![npm version](https://img.shields.io/npm/v/body-parser-xml)](https://img.shields.io/npm/v/body-parser-xml)
[![npm downloads per week](https://img.shields.io/npm/dw/body-parser-xml?color=blue)](https://img.shields.io/npm/dw/body-parser-xml?color=blue)
[![Dependency Status](https://david-dm.org/fiznool/body-parser-xml.svg)](https://david-dm.org/fiznool/body-parser-xml)
[![devDependency Status](https://david-dm.org/fiznool/body-parser-xml/dev-status.svg)](https://david-dm.org/fiznool/body-parser-xml#info=devDependencies)

## Installation

```
npm install express body-parser body-parser-xml
```

## Usage

This library adds an `xml` method to the `body-parser` object.

Initialise like so:

```js
const bodyParser = require('body-parser');
require('body-parser-xml')(bodyParser);
```

Once initialised, you can use it just like any other `body-parser` middleware:

```js
const app = require('express')();
app.use(bodyParser.xml());
```

This will parse any XML-based request and place it as a JavaScript object on `req.body` for your route handlers to use.

An XML-based request is determined by the value of the `Content-Type` header. By default, any `Content-Type` header ending in `/xml` or `+xml` will be parsed as XML. For example, the following Content-Types will all match:

- `text/xml`
- `application/xml`
- `application/rss+xml`

If you need to match against a custom `Content-Type` header, pass in the `type` to match as an option (see below).

### Options

You can also pass in options:

```js
app.use(bodyParser.xml(options));
```

The `options` object accepts any of the following keys:

#### defaultCharset

Specify the default character set for the text content if the charset is not specified in the `Content-Type` header of the request. Defaults to `utf-8`.

#### inflate

When set to `true`, then deflated (compressed) bodies will be inflated; when `false`, deflated bodies are rejected. Defaults to `true`.

#### limit

Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults to `'100kb'`.

#### type

The type option is used to determine what media type the middleware will parse. This option can be a string, array of strings, or a function. If not a function, type option is passed directly to the type-is library and this can be an extension name (like xml), a mime type (like application/xml), or a mime type with a wildcard (like _/_ or _/xml). If a function, the type option is called as fn(req) and the request is parsed if it returns a truthy value. Defaults to `['_/xml', '+xml']`.

#### verify

The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error.

#### xmlParseOptions

This option controls the behaviour of the XML parser. You can pass any option that is supported by the [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js) library: [see here](https://github.com/Leonidas-from-XIV/node-xml2js#options) for a list of these options.

## Example

```js
const express = require('express');
const bodyParser = require('body-parser');

require('body-parser-xml')(bodyParser);

const app = express();
app.use(
  bodyParser.xml({
    limit: '1MB', // Reject payload bigger than 1 MB
    xmlParseOptions: {
      normalize: true, // Trim whitespace inside text nodes
      normalizeTags: true, // Transform tags to lowercase
      explicitArray: false, // Only put nodes in array if >1
    },
  }),
);

app.post('/users', function (req, res, body) {
  // Any request with an XML payload will be parsed
  // and a JavaScript object produced on req.body
  // corresponding to the request payload.
  console.log(req.body);
  res.status(200).end();
});
```

## Motivation

This library was born out of a frustration that [express-xml-bodyparser](https://github.com/macedigital/express-xml-bodyparser), the most popular XML-parsing library for express, doesn't support the regular `body-parser` options - in particular, limiting the payload size.

This library was written to use `body-parser`'s text parser under the hood, and then passes the parsed string into the XML parser. We can therefore take advantage of `body-parser`'s regular options, and support limiting the payload size, amongst other things.

## License

MIT

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