3.6.3 • Published 1 year ago

xml-formatter v3.6.3

Weekly downloads
49,592
License
MIT
Repository
github
Last release
1 year ago

xml-formatter

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.

Build Status npm version

Installation

$ npm install xml-formatter

Example

Usage:

import xmlFormat from 'xml-formatter';

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

Output:

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

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. <tag />.
    • 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

Usage:

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'
});

Output:

<root>
  <content>
    <p>This is <b>some</b> content.</p>
  </content>
</root>

Minify mode

Usage:

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
});

Output:

<root><content><p>This is<b>some</b>content.</p></content></root>

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:

const xmlFormatter = require('xml-formatter');

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

Using global function xmlFormatter

Page:

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

Usage:

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

Output

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

License

MIT

@edifice.io/mindplot@rajbandi/wsdlstubgen@aasaam/pwa-toolsxml-updaterscss2kuipdiff-xml-layout@springworks/android-plankanhttpyacdobda-converteresfmldocusaurus-plugin-openapi-docs-slashiddocusaurus-theme-openapi-docs-slashidreact-native-slider-kf@tisf/bp-initluck-design-testsfpowerkit@pxwlab/sprite-cli@pxwlab/katana-builder@everything-registry/sub-chunk-3186pxw-sprite-cliwe-editwebstorm-cleanxmind2jira-clixmind2jsonxmind2json-clixmindparserxml-editor-libraryxml-localesxliff-auto-translatexml-update-processwsdlstubgensvekyll-clivite-plugin-pages-sitemap@benbird/cf_edit@benbird/bf_design@romain325/elements-core-webcomponentsitrace-atomjest-serializer-xmljsf1kasayainfo-epguifierhdoc-toolshp-taqe-rmgzl-datahouse-renderershardwario-blockly-devliferay-gulp-tasksluck-reactluck-designmd2oedxkoji-langkodi-tvshow-nfo-generatormaximo-dev-toolsmd2anmd2an-for-sitemd-to-ssl@whalecloud/page-builder@wisemapping/mindplot@wdz996/ssml-editor@types/xml-formatter@timdp/pomeranian@twec/netsuite-client@twec/node-suite@tokenscript/cli@tisf/rn-init@tisf/scripts@tisf/utils@tisf/rn-setup@up9/mizu-common@vishalbd/docusaurus-plugin-openapi-docs@virtual-beans/editor@yakies/graph-model@yakies/models@yakies/rx-socket-client@yakies/rx-socket-serverarchive-watcherariel-diagramsapitoxml@chrizuuu/react-native-bootsplash@cismet/csconf@azure-tools/cadl-ranch-api@citolab/qti-convert@abbudao/elements-core@guticoma/html-to-mbn@eccenca/gui-elements@adobe/helix-importer-jcr-packaging@adobe/helix-md2jcr@adobe/aem-cs-source-migration-index-converter@edifice-wisemapping/mindplot@creativepenguin/format-xml@hawtio/react@flxblio/sfprofilessandukreact-native-bootscreenspidosrt2fcpxmlssmdtxgw-docusaurus-plugin-openapi-docstxgw-docusaurus-theme-openapi-docsumi-plugin-cordova-enhance
3.6.3

1 year ago

3.6.2

2 years ago

3.6.1

2 years ago

3.6.0

2 years ago

3.5.0

2 years ago

3.4.0

2 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.1

3 years ago

3.4.1

2 years ago

3.3.2

3 years ago

3.2.0

3 years ago

3.0.2

3 years ago

3.1.0

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.6.1

4 years ago

2.6.0

4 years ago

2.5.0

4 years ago

2.5.1

4 years ago

2.4.1

4 years ago

2.4.0

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.2.1

6 years ago

1.2.0

6 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