Simple XML formatting/parsing
Installation of the npm package:
npm install --save @vates/xml
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 & 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: [] }
// ]
// }
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 or0
, 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 & entities
// <tag2 />
// <tag2 />
// <ns:tag3 />
// </tag1>
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.