3.6.3 • Published 5 months ago

xml-formatter v3.6.3

Weekly downloads
49,592
License
MIT
Repository
github
Last release
5 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-processwsdlstubgensfdx-essentialssfdx-hydratesfdx-papers-pleasesf-codegensaml-idp-newsaml-idp-test-serversaml-idpscriptworks-clientrest-cliseasun-designscss2kuip-toolsvekyll-clivite-plugin-pages-sitemap@flxblio/sfprofilessandukreact-native-bootscreenliferay-gulp-tasksjest-serializer-xmljsf1itrace-atomkoji-langkodi-tvshow-nfo-generatorguifiergrunt-xml-formatterhp-taqe-rminfo-epgpx2kmlgoogle-takeout-location-history-parsergotrongzl-datahouse-rendererskasayahdoc-toolshardwario-blockly-devmaximo-dev-toolsmd2oedxmd-to-sslmd2anmd2an-for-siteluck-designluck-reactpage-builder-zhang-testosis2jsonpptx-surgeonodata-hammeromega-mloldprofile-modifier-pluginprofile-modifier-pluginnext-with-sitemapnode-rayreact-native-boot-screenreact-native-bootsplashreact-native-bootsplash-papareact-render-staticreact-ssml-editornubikanext-raynetconfigconstructnetconfjs-mehobojobo-document-json-parserspidosrt2fcpxmlssmdslide-diffsfpowerkit-kp-clismart-mjmlsitemap-tssitemap-ts-advancedsketch-to-svgtxgw-docusaurus-plugin-openapi-docs
3.6.3

5 months ago

3.6.2

10 months ago

3.6.1

10 months ago

3.6.0

1 year ago

3.5.0

1 year 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

3 years ago

2.5.1

3 years ago

2.4.1

3 years ago

2.4.0

4 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

5 years ago

2.0.0

5 years ago

1.2.1

5 years ago

1.2.0

5 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