
Product
Introducing Reports: An Extensible Reporting Framework for Socket Data
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.
xml-formatter
Advanced tools
Converts a XML string into a human readable format (pretty print) while respecting the xml:space attribute
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 that are used as attribute delimiters.
'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
The pretty-data package provides similar functionality for formatting XML, JSON, and CSS data. It offers a broader range of data formatting options compared to xml-formatter, which is specialized for XML.
The xml2js package is primarily used for converting XML to JavaScript objects and vice versa. While it does offer some formatting capabilities, its main focus is on XML parsing and building, making it more versatile for data manipulation tasks.
The xmlbuilder package is designed for building XML documents using a simple and chainable API. It also includes formatting options, but its primary strength lies in creating XML documents programmatically.
FAQs
Converts a XML string into a human readable format (pretty print) while respecting the xml:space attribute
The npm package xml-formatter receives a total of 976,797 weekly downloads. As such, xml-formatter popularity was classified as popular.
We found that xml-formatter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Explore exportable charts for vulnerabilities, dependencies, and usage with Reports, Socket’s new extensible reporting framework.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.