3.6.2 • Published 3 months ago

xml-formatter v3.6.2

Weekly downloads
49,592
License
MIT
Repository
github
Last release
3 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-clidev-blog-directory-readme-generatorseasun-designscss2kuip-toolscriptworks-clientsfdx-essentialssfdx-hydratesfdx-papers-pleasesf-codegenspidoep-wsssmdsrt2fcpxmldhl-nodedota2-panorama-xml-static-elementreact-native-bootscreenreact-native-bootsplashreact-native-bootsplash-papareact-render-staticdocusaurus-theme-openapi-docsdocusaurus-theme-openapi-docs-xvladqtdocusaurus-plugin-openapi-docsdocusaurus-plugin-openapi-docs-xvladqtcreatemassaml-idpsaml-idp-newsaml-idp-test-serversandukrest-clireact-ssml-editorslide-diffelement-plus-form-testelements-core-testelite3eelite3e1elite3e2elite3e3elite3e33elite3e4elite3e5elite3e6elite3e8elite3e9ecatmodsmart-mjmlcsconfsitemap-tssitemap-ts-advancedsketch-to-svg@bitvalser/animegen@benbird/bf_design@benbird/cf_edit@chrizuuu/react-native-bootsplash@cismet/csconf@citolab/qti-convert@chhooo/docusaurus-plugin-openapi-docs@spreading/openapi-docs-plugin@slashid/docusaurus-plugin-openapi-docs-slashid@slashid/docusaurus-theme-openapi-docs-slashid@svelte-eui/ecl-eui-svelte@sw7d/ssml-editor@swishjs/base@creativepenguin/format-xml@blockfrost/docusaurus-theme-openapi-docsbreadbutterbreadmachinebmmmmblocks-docsblockly-test-nodejsboostcode@asfandsh/rn-setupcucumber-js-extentdaisy-cell-block-diagram@abbudao/elements-coreflaygroundflupsvekyll-clitisf@sidewinder1138/saml-idp@up9/mizu-common@vishalbd/docusaurus-plugin-openapi-docs
3.6.2

3 months ago

3.6.1

3 months ago

3.6.0

6 months ago

3.5.0

9 months ago

3.4.0

11 months ago

3.3.1

1 year ago

3.3.0

1 year ago

3.2.1

1 year ago

3.4.1

11 months ago

3.3.2

1 year ago

3.2.0

1 year ago

3.0.2

1 year ago

3.1.0

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.6.1

2 years ago

2.6.0

2 years ago

2.5.0

3 years ago

2.5.1

2 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.2.1

4 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago