An open API service indexing awesome lists of open source software.

https://github.com/t1st3/muxml

:saxophone: Streaming XML parser and formatter based on sax
https://github.com/t1st3/muxml

formatter parser stream xml

Last synced: 4 months ago
JSON representation

:saxophone: Streaming XML parser and formatter based on sax

Awesome Lists containing this project

README

          

# muxml [![Build Status Travis](https://travis-ci.org/t1st3/muxml.svg?branch=master)](https://travis-ci.org/t1st3/muxml) [![Coverage Status](https://codecov.io/gh/t1st3/muxml/badge.svg?branch=master)](https://codecov.io/gh/t1st3/muxml?branch=master)

> Streaming XML parser and formatter

`muxml` is a [Transform stream](https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams) that accepts XML strings, and emits small chunks containing individual XML elements.

The parser part of this module is built on [`sax`](https://www.npmjs.com/package/sax):saxophone:.
On parsing, the stream is broken-up and reassembled so that each opening/closing tag and each text is a chunk. During this process, sax events are re-emitted and are listenable down the pipeline.

Once XML is parsed, this module can optionally format XML in the following fashions:

* XML minification (de-indentation and removal of newlines)
* XML beautification (indentation and usage of newlines)
* striping of comments
* striping of `CDATA`
* striping of instruction (like ``) tags
* striping of `= 4
* in the browser, as global script
* in the browser, with an AMD loader

Also available as a [gulp](https://github.com/t1st3/gulp-muxml) / [Grunt](https://github.com/t1st3/grunt-muxml) plugin, or as a [CLI](https://github.com/t1st3/muxml-cli) app.

## Install

```
$ npm install --save muxml
```

## Usage

Suppose a file named `example.xml` containing the follwing XML

```xml


d


```

then, `muxml()` returns a transform stream that accepts XML strings and emits XML elements as strings

```js
const muxml = require('muxml');

fs.createReadStream('example.xml')
.pipe(muxml({pretty: false}))
.on('data', function (data) {
console.log(data);
//=> 'd'
});
```

## Options

### strict

Type: `boolean`

Default: `true`

Set [`sax` parser `strict` argument](https://www.npmjs.com/package/sax#arguments)

### pretty

Type: `boolean`

Default: `true`

Prettify the output. If true, output has newlines and indentation.

### indentStyle

Type: `string`

Default: `spaces`

When `pretty` is set to true, indent with either `spaces` or `tabs`.

### indentSpaces

Type: `integer`

Default: `2`

When `pretty` is set to true and `indentStyle` is set to `spaces`, then indent with this number of spaces.

### indentTabs

Type: `integer`

Default: `1`

When `pretty` is set to true and `indentStyle` is set to `tabs`, then indent with this number of tabs.

#### filter

Type: `string`

Default: `null`

a filter for tag names

Filter XML with tags with name matching the filter.

### stripAttributes

Type: `boolean`

Default: `false`

Strip attributes from tags.

### stripCdata

Type: `boolean`

Default: `true`

Strip `CDATA` tags.

### stripComments

Type: `boolean`

Default: `true`

Strip XML comments.

### stripDoctype

Type: `boolean`

Default: `true`

Strip `
Default: `true`

Strip processing instruction (like ``) tags.

### saxOptions

Type: `object`

Default: `{}`

Set [options of `sax` parser](https://www.npmjs.com/package/sax#arguments). Note that `trim` can not be set to `false`.

## API

### muxml(options)

#### options

Type: `object`

as described above

## Related

* [muxml-cli](https://github.com/t1st3/muxml-cli) | CLI for this module
* [gulp-muxml](https://github.com/t1st3/gulp-muxml) | this module as a [`gulp`](http://gulpjs.com/) plugin
* [grunt-muxml](https://github.com/t1st3/grunt-muxml) | this module as a [`Grunt`](http://gruntjs.com/) plugin

## License

MIT © [t1st3](https://t1st3.com)