JSPM

  • Created
  • Published
  • Downloads 564959
  • Score
    100M100P100Q181352F
  • License MIT

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

Package Exports

  • xml-formatter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (xml-formatter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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).

Travis CI 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 options = {indentation: '  ', stripComments: true, collapseContent: true};
var formattedXml = format(xml, options);

console.log(formattedXml);

Output:

<root>
  <content>
    <p>This is <b>some</b> content.</p>
  </content>
</root>
  • stripComments (Boolean, default=true) True to strip the comments.
  • indentation (String, default=' ') The value used for indentation.
  • debug (Boolean, default=false) Displays a tree of the parsed XML before formatting.
  • 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

On The Browser

Page:

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

Usage:

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>

License

MIT