Converts XML into a human readable format (pretty print) while respecting the xml:space attribute.
Reciprocally, the xml-formatter package can minify pretty printed XML.
The xml-formatter package can also be used on the browser using the browserified version with a small footprint.
$ npm install xml-formatter
import xmlFormat from 'xml-formatter';
xmlFormat('<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>');
<root>
<content>
<p xml:space="preserve">This is <b>some</b> content.</p>
</content>
</root>
filter: Function to filter out unwanted nodes by returning false.
function(node) => boolean() => trueignoredPaths: List of XML element paths to ignore during formatting. This can be a partial path (element tag name) or full path starting from the document element e.g. ['/html/head/script', 'pre'].
string[][]indentation: The value used for indentation.
string' 'collapseContent: True to keep content in the same line as the element. Only works if element contains at least one text node.
booleanfalselineSeparator: Specify the line separator to use.
string\r\nwhiteSpaceAtEndOfSelfclosingTag: True to end self-closing tags with a space e.g. <tag />.
booleanfalsethrowOnFailure: Throw an error when XML fails to parse and get formatted otherwise the original XML is returned.
booleantrueforceSelfClosingEmptyTag: True to force empty tags to be self-closing.
booleanfalseattributeQuotes: The quote characters to use as attribute delimiters. Valid options are 'single' fo single quotes and 'double' for double quotes.
'double|single''double'import xmlFormat from 'xml-formatter';
xmlFormat('<root><!-- content --><content><p>This is <b>some</b> content.</content></p>', {
indentation: ' ',
filter: (node) => node.type !== 'Comment',
collapseContent: true,
lineSeparator: '\n'
});
<root>
<content>
<p>This is <b>some</b> content.</p>
</content>
</root>
import xmlFormat from 'xml-formatter';
const xml = `
<root>
<content>
<p>
This is <b>some</b> content.
</p>
</content>
</root>`;
xmlFormat.minify(xml, {
filter: (node) => node.type !== 'Comment',
collapseContent: true
});
<root><content><p>This is<b>some</b>content.</p></content></root>
The code is transpiled using Babel with @babel/preset-env default values and bundled using browserify.
require('xml-formatter')<script type="text/javascript" src="dist/browser/xml-formatter.js"></script>
const xmlFormatter = require('xml-formatter');
xmlFormat('<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>');
xmlFormatter<script type="text/javascript" src="dist/browser/xml-formatter-singleton.js"></script>
xmlFormatter('<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>');
<root>
<content>
<p xml:space="preserve">This is <b>some</b> content.</p>
</content>
</root>
MIT
Overall
Maintained
Code review
Latest release
Releases this year
46 followers · starred Aug 2021
Converts XML into a human readable format (pretty print) while respecting the xml:space attribute.
Reciprocally, the xml-formatter package can minify pretty printed XML.
The xml-formatter package can also be used on the browser using the browserified version with a small footprint.
$ npm install xml-formatter
import xmlFormat from 'xml-formatter';
xmlFormat('<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>');
<root>
<content>
<p xml:space="preserve">This is <b>some</b> content.</p>
</content>
</root>
filter: Function to filter out unwanted nodes by returning false.
function(node) => boolean() => trueignoredPaths: List of XML element paths to ignore during formatting. This can be a partial path (element tag name) or full path starting from the document element e.g. ['/html/head/script', 'pre'].
string[][]indentation: The value used for indentation.
string' 'collapseContent: True to keep content in the same line as the element. Only works if element contains at least one text node.
booleanfalselineSeparator: Specify the line separator to use.
string\r\nwhiteSpaceAtEndOfSelfclosingTag: True to end self-closing tags with a space e.g. <tag />.
booleanfalsethrowOnFailure: Throw an error when XML fails to parse and get formatted otherwise the original XML is returned.
booleantrueforceSelfClosingEmptyTag: True to force empty tags to be self-closing.
booleanfalseattributeQuotes: The quote characters to use as attribute delimiters. Valid options are 'single' fo single quotes and 'double' for double quotes.
'double|single''double'import xmlFormat from 'xml-formatter';
xmlFormat('<root><!-- content --><content><p>This is <b>some</b> content.</content></p>', {
indentation: ' ',
filter: (node) => node.type !== 'Comment',
collapseContent: true,
lineSeparator: '\n'
});
<root>
<content>
<p>This is <b>some</b> content.</p>
</content>
</root>
import xmlFormat from 'xml-formatter';
const xml = `
<root>
<content>
<p>
This is <b>some</b> content.
</p>
</content>
</root>`;
xmlFormat.minify(xml, {
filter: (node) => node.type !== 'Comment',
collapseContent: true
});
<root><content><p>This is<b>some</b>content.</p></content></root>
The code is transpiled using Babel with @babel/preset-env default values and bundled using browserify.
require('xml-formatter')<script type="text/javascript" src="dist/browser/xml-formatter.js"></script>
const xmlFormatter = require('xml-formatter');
xmlFormat('<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>');
xmlFormatter<script type="text/javascript" src="dist/browser/xml-formatter-singleton.js"></script>
xmlFormatter('<root><content><p xml:space="preserve">This is <b>some</b> content.</content></p>');
<root>
<content>
<p xml:space="preserve">This is <b>some</b> content.</p>
</content>
</root>
MIT
Overall
Maintained
Code review
Latest release
Releases this year
46 followers · starred Aug 2021