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

https://github.com/teclone/xml-serializer

xml-serializer is a complete JavaScript implementation of the W3C xml serialization specifications
https://github.com/teclone/xml-serializer

dom-parser jsdom npm w3c-specification xml-serialization xml-serializer

Last synced: 10 months ago
JSON representation

xml-serializer is a complete JavaScript implementation of the W3C xml serialization specifications

Awesome Lists containing this project

README

          

# XML-Serializer

[![Build Status](https://travis-ci.org/teclone/xml-serializer.svg?branch=master)](https://travis-ci.org/teclone/xml-serializer)
[![Coverage Status](https://coveralls.io/repos/github/teclone/xml-serializer/badge.svg?branch=master)](https://coveralls.io/github/teclone/xml-serializer?branch=master)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![npm version](https://badge.fury.io/js/%40teclone%2Fxml-serializer.svg)](https://badge.fury.io/js/%40teclone%2Fxml-serializer)
![npm](https://img.shields.io/npm/dt/%40teclone%2Fxml-serializer.svg)

XML-Serializer is a complete JavaScript implementation of the W3C [xml serialization](https://www.w3.org/TR/DOM-Parsing/#dfn-concept-serialize-xml) specifications. All specifications have been implemented and includes the following [specs](https://www.w3.org/TR/DOM-Parsing/#dfn-concept-xml-serialization-algorithm):

- [ELEMENT_NODE Serialization]

- [DOCUMENT_NODE Serialization]

- [COMMENT_NODE Serialization]

- [TEXT_NODE Serialization]

- [DOCUMENT_FRAGMENT_NODE Serialization]

- [DOCUMENT_TYPE_NODE Serialization]

- [PROCESSING_INSTRUCTION_NODE Serialization]

## Module Availability

This module is available as an [npm](https://www.npmjs.com/package/@teclone/xml-serializer) scoped package and also has a browser build that is located inside the `dist` folder. It can easily be integrated with [JSDOM](https://github.com/jsdom/jsdom) for mockup testing.

## Getting Started

The below command will install `xml-serializer` from npm into your project assuming you have the [npm](https://www.npmjs.com/) already installed.

**Install as a development dependency**:

```bash
npm install --save-dev @teclone/xml-serializer
```

## Usage Guide

Following the specification, the `XMLSerializer` interface is a constructor and has a `serializeToString(root)` method exposed on the instance. To serialize any xml node, call the `serializeToString(root)` method on a constructed instance, passing in the xml node as like shown below:

```javascript
import XMLSerializer from '@teclone/xml-serializer';

const instance = new XMLSerializer();
console.log(instance.serializeToString(someXmlNode));
```

The constructor can take a boolean argument that indicates if whitespace should be preserved in the serialized output. Default value is `true`;

```javascript
// do not preserve white space
const instance = new XMLSerializer(false);
const xmlString = instance.serializeToString(document);
```

### Using with [JSDOM](https://github.com/jsdom/jsdom)

Currently [at the time of creating this], JSDOM has not implemented the `XMLSerializer` interface. This can be easily integrated with JSDOM and any other similar mockup environment or for web scrapping and xml feed parsing like below.

```javascript
//assumes jsdom has been installed.
import XMLSerializer from '@teclone/xml-serializer';
import { JSDOM } from 'jsdom';

const dom = new JSDOM();
XMLSerializer.installTo(dom.window);

global.window = dom.window;

//start running your tests or do something else.
```

### Using on the browser

The browser build is available inside the `build/dist` folder when you npm install the package. You can also clone this repo and run the build command locally. It exposes an `XMLSerializer` construct on the `window` object.

```html

<script>
<script type="text/javascript">
const serializer = new XMLSerializer();
// do some serialization stuffs

```

## Features & Improvements

By default, the serializer preserves white space during the serialization process. This can be turned off if you want a compact output by passing in `false` to the constructor at the time of creating an instance.

```javascript
//do not preserve white space
const instance = new XMLSerializer(false);
```

Another improvement is that it removes all duplicate xml prefix definition on as recommended in the specification document unlike what web browsers do. Below is an example of
this:

**Original XML**:

```xml





Apples
Bananas



African Coffee Table
80
120








this is a paragraph text





this is a template






my title

```

**Chrome inbuilt XMLSerializer Output:**

Notice that none of the duplicated namespaces is removed.

```xml





Apples
Bananas



African Coffee Table
80
120








this is a paragraph text





this is a template






my title

```

**Output of this module:**

Notice that all of the duplicated namespaces are removed.

```xml





Apples
Bananas



African Coffee Table
80
120








this is a paragraph text





this is a template






my title

```

## Contributing

We welcome your own contributions, ranging from code refactoring, documentation improvements, new feature implementations, bugs/issues reporting, etc. We recommend you follow the steps below to actively contribute to this project:

1. Decide on what to help us with.

2. Fork this repo to your machine.

3. Implement your ideas, and once stable,

4. Create a pull request, explaining your improvements/features