Converts a XML string into a human readable format (pretty print) while respecting the xml:space attribute
npm install xml-formatterConverts 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
`js
import xmlFormat from 'xml-formatter';
xmlFormat(' This is some content.
$3
`xml
This is some content.
`Options
-
filter: Function to filter out unwanted nodes by returning false.
- type: function(node) => boolean
- default: () => true
- ignoredPaths: 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'].
- type: string[]
- default: []
- indentation: The value used for indentation.
- type: string
- default: ' '
- collapseContent: True to keep content in the same line as the element. Only works if element contains at least one text node.
- type: boolean
- default: false
- lineSeparator: Specify the line separator to use.
- type: string
- default: \r\n
- whiteSpaceAtEndOfSelfclosingTag: True to end self-closing tags with a space e.g. .
- type: boolean
- default: false
- throwOnFailure: Throw an error when XML fails to parse and get formatted otherwise the original XML is returned.
- type: boolean
- default: true
- forceSelfClosingEmptyTag: True to force empty tags to be self-closing.
- type: boolean
- default: false$3
`js
import xmlFormat from 'xml-formatter';xmlFormat('This is some content.
', {
indentation: ' ',
filter: (node) => node.type !== 'Comment',
collapseContent: true,
lineSeparator: '\n'
});`$3
`xml
This is some content.
`Minify mode
$3
`js
import xmlFormat from 'xml-formatter';const xml =
This is some content.
;xmlFormat.minify(xml, {
filter: (node) => node.type !== 'Comment',
collapseContent: true
});
`$3
`xml
This issomecontent.
`On The Browser
The code is transpiled using Babel with @babel/preset-env default values and bundled using browserify.
$3
$3
`html
`$3
`js
const xmlFormatter = require('xml-formatter');xmlFormat('This is some content.
');
`$3
$3
`html
`$3
`js
xmlFormatter('This is some content.
');
`$3
`xml
This is some content.
``MIT