@vates/xml

2.0.0 • Public • Published

@vates/xml

Package Version License PackagePhobia Node compatibility

Simple XML formatting/parsing

Install

Installation of the npm package:

npm install --save @vates/xml

Usage

parseXml(xml, [opts])

Based on sax

opts is an optional object which can contain the following options:

  • normalize = true: if true, then turn any whitespace into a single space
  • strict = true: whether or not to be a jerk
  • trim = true: whether or not to trim text nodes
import { parseXml } from '@xen-orchestra/xml/parse'

const tree = parseXml(`<?xml version="1.0" encoding="UTF-8"?>
<tag1 attr1="value1" attr2="value2">
  Text &amp; entities
  <tag2 />
  <tag2 />
  <ns:tag3 />
</tag1>
`)
// → {
//   name: 'tag1',
//   attributes: { attr1: 'value1', attr2: 'value2' },
//   children: [
//     'Text & entities',
//     { name: 'tag2', attributes: {}, children: [] },
//     { name: 'tag2', attributes: {}, children: [] },
//     { name: 'ns:tag3', attributes: {}, children: [] }
//   ]
// }

formatXml(tree, [opts])

opts is an optional object which can contain the following options:

  • includeDeclaration = true: whether to include an XML declaration
  • indent = 2: string or number of spaces to use to indent; if an empty string or 0, no indent or new lines will be used
import { formatXml } from '@xen-orchestra/xml/format'

formatXml({
  name: 'tag1',
  attributes: { attr1: 'value1', attr2: 'value2' },
  children: ['Text & entities', { name: 'tag2' }, { name: 'tag2' }, { name: 'ns:tag3' }],
})
// → <?xml version="1.0" encoding="UTF-8"?>
// <tag1 attr1="value1" attr2="value2">
//   Text &amp; entities
//   <tag2 />
//   <tag2 />
//   <ns:tag3 />
// </tag1>

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Vates SAS

Readme

Keywords

none

Package Sidebar

Install

npm i @vates/xml

Weekly Downloads

104

Version

2.0.0

License

ISC

Unpacked Size

6.04 kB

Total Files

5

Last publish

Collaborators

  • stephane-m-dev
  • florent.beauchamp
  • mathieura
  • julien-f
  • marsaud
  • olivierlambert
  • pdonias
  • tgoettelmann
  • enishowk
  • b-nollet
  • mlssfrncjrg
  • arnogues
  • joris-k
  • sebastianczech