xml-formatter
Converts a XML string into a human readable format (pretty print) while respecting the xml:space attribute
Last updated 4 years ago by chrisbottin .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install xml-formatter 
SYNC missed versions from official npm registry.

xml-formatter

Converts XML into a human readable format (pretty print) while respecting the xml:space attribute.

This module can also be used on the browser using the browserified version with a small footprint (8KB file size).

Build Status

NPM

Installation

$ npm install xml-formatter

Example

JavaScript:

var format = require('xml-formatter');
var xml = '<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>';

var formattedXml = format(xml);
console.log(formattedXml);

Output:

<root>
    <content>
        <p xml:space="preserve">This is <b>some</b> content.</p>
    </content>
</root>

Options

JavaScript:

var format = require('xml-formatter');
var xml = '<root><!-- content --><content><p>This is <b>some</b> content.</content></p>';

var formattedXml = format(xml, {
    indentation: '  ', 
    filter: (node) => node.type !== 'Comment', 
    collapseContent: true, 
    lineSeparator: '\n'
});

console.log(formattedXml);

Output:

<root>
  <content>
    <p>This is <b>some</b> content.</p>
  </content>
</root>
  • filter (function(node)) Function to filter out unwanted nodes by returning false.
  • indentation (String, default=' ') The value used for indentation.
  • collapseContent (Boolean, default=false] True to keep content in the same line as the element. Only works if element contains at least one text node
  • lineSeparator (String, default=\r\n) Specify the line separator to use
  • whiteSpaceAtEndOfSelfclosingTag (Boolean, default=false) to either end ad self closing tag with <tag/> or <tag />

On The Browser

The code is transpiled using Babel with @babel/preset-env default values and bundled using browserify.

Using require('xml-formatter')

Page:

<script type="text/javascript" src="dist/browser/xml-formatter.js"></script>

Usage:

var xmlFormatter = require('xml-formatter');
var xml = '<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>';

var formattedXml = xmlFormatter(xml);
console.log(formattedXml);

Using global function xmlFormatter

<script type="text/javascript" src="dist/browser/xml-formatter-singleton.js"></script>

Usage:

var xml = '<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>';

var formattedXml = xmlFormatter(xml);
console.log(formattedXml);

Output

<root>
    <content>
        <p xml:space="preserve">This is <b>some</b> content.</p>
    </content>
</root>

License

MIT

Current Tags

  • 2.6.1                                ...           latest (4 years ago)

22 Versions

  • 2.6.1                                ...           4 years ago
  • 2.6.0                                ...           4 years ago
  • 2.5.1                                ...           4 years ago
  • 2.5.0                                ...           4 years ago
  • 2.4.1                                ...           5 years ago
  • 2.4.0                                ...           5 years ago
  • 2.3.1                                ...           5 years ago
  • 2.3.0                                ...           6 years ago
  • 2.2.0                                ...           6 years ago
  • 2.1.3                                ...           6 years ago
  • 2.1.2                                ...           6 years ago
  • 2.1.1                                ...           6 years ago
  • 2.1.0                                ...           6 years ago
  • 2.0.1                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.2.1                                ...           6 years ago
  • 1.2.0                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.1                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
  • 0.0.2                                ...           9 years ago
  • 0.0.1                                ...           9 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (8)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |