3.6.3 • Published 10 months ago

xml-formatter v3.6.3

Weekly downloads
49,592
License
MIT
Repository
github
Last release
10 months 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@creativepenguin/format-xml@d1vai/stoplight-elements-core@diplodoc/translation@benbird/cf_edit@benbird/bf_design@romain325/elements-core-webcomponentsitrace-atomjest-serializer-xmljsf1kasaya@flxblio/sfprofilessandukreact-native-bootscreenspidosrt2fcpxmlssmdtxgw-docusaurus-plugin-openapi-docstxgw-docusaurus-theme-openapi-docsumi-plugin-cordova-enhancetheme-openapi-writedocsthree-svg-renderertisftoms-parameter@asfandsh/rn-setup@adobe/aem-cs-source-migration-index-converter@azure-tools/cadl-ranch-api@bitvalser/animegen@blockfrost/docusaurus-theme-openapi-docs@cashub/ui@cashub/utils@citolab/qti-convert@chrizuuu/react-native-bootsplash@cheny33/ssml-editor@chhooo/docusaurus-plugin-openapi-docs@cismet/csconf@abbudao/elements-coreyss-image-annotateyzpf-dw-clixsdlibrarywc-to-aemts-glittervector-drawable-svg@edifice-wisemapping/mindplot@hawtio/react@guticoma/html-to-mbn@heduapp/book-objects@flatbiz/antd@hitachivantara/uikit-react-code-editor@hernas/docusaurus-plugin-openapi-docs@hernas/docusaurus-theme-openapi-docs@flxbl-io/sfprofiles@floriandorau/httplog@flowbuild/nodejs-diagram-builder@elumixor/translator@gdyfe/ssml@fye/netsuite-client@gip-recia/wisemapping-mindplot@indexyz/bumblebee@infinitebrahmanuniverse/nolb-xm@kronsi/dhl@lambo-design/monaco-editor@l10nmonster/helpers-android@metztheolab/xml-editor-library@mtntop/utils@mxenabled/docusaurus-plugin-openapi-docs@mxenabled/docusaurus-theme-openapi-docs@nfps.dev/sdk
3.6.3

10 months ago

3.6.2

1 year ago

3.6.1

1 year ago

3.6.0

2 years ago

3.5.0

2 years ago

3.4.0

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.2.1

2 years ago

3.4.1

2 years ago

3.3.2

2 years ago

3.2.0

2 years ago

3.0.2

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.0

4 years ago

2.5.1

4 years ago

2.4.1

4 years ago

2.4.0

4 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

5 years ago

1.2.1

5 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago